I'm currently having trouble getting the correct value from the spinbox widget. I do not know what is wrong. I have searched for solutions and have come empty. What am I doing wrong? Here is my code:
from Tkinter import *
#create Tk window
root = Tk()
#set name of window
root.title('Testing Values')
#initalise values from user (spinbox values)
item_1 = IntVar()
a = item_1.get()
def print_item_values():
global a
print a
#item 1 spinbox
item_1 = Spinbox(root, from_= 0, to = 10, width = 5)
item_1.grid(row = 0, column = 0)
#print values
value_button = Button(root, text = 'Print values', width = 10, command = print_item_values)
value_button.grid(row = 0, column = 1)
root.mainloop()
A Spinbox has an area for showing the current value and a pair of arrowheads. When you click the upward-pointing arrowhead, the Spinbox advances the current value to the next higher value in the sequence. If the current value reaches the maximum value, you can set it to the minimum value.
The Spinbox widget is an alternative to the Entry widget. It provides the range of values to the user, out of which, the user can select the one. It is used in the case where a user is given some fixed number of values to choose from.
We can set the default value for the spinbox widget by defining the Value using StringVar() object. The default value plays a vital role for any widget, as it helps you to define the bounded value.
Enable disable Spinbox To disable we can use option state or use config().
In your code, a
is never updated. Instead, to get the spinbox value, just use its .get()
method:
item_1 = Spinbox(root, from_= 0, to = 10, width = 5)
item_1.grid(row = 0, column = 0)
def print_item_values():
print item_1.get()
Tkinter Spinbox Documentation
You can make following changes in your code
from tkinter import *
root = Tk()
root.title('Testing Values')
item_1 = IntVar()
def print_item_values():
a = item_1.get()
print(a)
item_1 = Spinbox(root, from_= 0, to = 10, width = 5)
item_1.grid(row = 0, column = 0)
value_button = Button(root, text = 'Print values', width = 10, command =
print_item_values)
value_button.grid(row = 0, column = 1)
root.mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With