- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set a 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 −
- Related Articles
- Tkinter Spinbox Widget Setting Default Value
- Set a default value for a ttk Combobox in Tkinter?
- How to set default text for a Tkinter Entry widget?
- How to set a default parameter value for a JavaScript function?
- How can I set the default value of my Tkinter Scale widget slider to 100?
- How to set MySQL default value NONE?
- How to set default Field Value in MySQL?
- How to set default value to NULL in MySQL?
- Set default value to a JSON type column in MySQL?
- How to set default value to align content in CSS ?
- How do I set the default value for a column in MySQL?
- How to set justification on Tkinter Text box?
- How to set default value for empty row in MySQL?
- How to set a default colormap in Matplotlib?
- How to set a Tkinter window to a constant size?
