w3resource

Python tkinter widgets Exercise: Create a Spinbox widget using tkinter module

Python tkinter widgets: Exercise-6 with Solution

Write a Python GUI program to create a Spinbox widget using tkinter module.

Sample Solution:

Python Code:

import tkinter as tk
root = tk.Tk()
text_var = tk.DoubleVar()

spin_box = tk.Spinbox(
    root,
    from_=0.6,
    to=50.0,
    increment=.01,
    textvariable=text_var
)
spin_box.pack()
root.mainloop()

Explanation:

In the exercise above -

  • import tkinter as tk - Import the Tkinter library (tkinter).
  • root = tk.Tk() - Create the main Tkinter window (root).
  • text_var = tk.DoubleVar() - Create a DoubleVar (text_var) to hold a double (floating-point) variable.
  • spin_box = tk.Spinbox( root, from_=0.6, to=50.0, increment=.01, textvariable=text_var) - Create a Spinbox widget (spin_box) for selecting numeric values. Set the allowed range for values (from 0.6 to 50.0) and the increment step (0.01). Associate the DoubleVar (text_var) with the Spinbox using textvariable.
  • spin_box.pack() - Display the Spinbox within the main window.
  • root.mainloop() - Start the Tkinter main loop to run the GUI application.

Sample Output:

Tkinter: Create a Spinbox widget using tkinter module

Flowchart:

Flowchart: Create a Spinbox widget using tkinter module

Python Code Editor:


Previous: Write a Python GUI program to create a Checkbutton widget using tkinter module.
Next: Write a Python GUI program to create a Text widget using tkinter module. Insert a string at the beginning then insert a string into the current text. Delete the first and last character of the text.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/tkinter/python-tkinter-widgets-exercise-6.php