
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set Default String Value on a Tkinter Spinbox
To set a default string value on a Tkinter Spinbox, we will have to use the set method. Let us take an example and see how to create a spinbox with a set of string values and then set a default string.
Steps −
Import the tkinter library and create an instance of tkinter frame.
Set the size of the frame using geometry method.
Create a set of strings and save it in a variable, data.
Next, use the StringVar() constructor to create a StringVar object. It helps to manage the value of a widget, which is Spingbox in this case. If you don't pass any parameters, then it defaults to the root window.
Create a spinbox and set its values by passing the data values.
Assign the StringVar object to the textvariable of the spinbox. Using textvariable, you can easily update the text of a widget.
Set a default value for the spinbox by using the set method. Here, we have picked a value from data and set it as default. var.set('Truck'). You can also use var.set(data[2]).
Finally, run the mainloop of the application window.
Example
from tkinter import * win = Tk() win.geometry('700x350') win.title('Spinbox') data = ['Car', 'Bus', 'Truck', 'Bike', 'Airplane'] var = StringVar(win) my_spinbox = Spinbox(win, values=data, textvariable=var, width=20, font="Calibri, 12") my_spinbox.pack(padx=20, pady=20) var.set('Truck') win.mainloop()
Output
Now, let's check its output −