我目前无法从Spinbox小部件中获取正确的值。我不知道怎么了。我一直在寻找解决方案,而且已经空了。我究竟做错了什么?这是我的代码:
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值,只需使用其.get()
方法:
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
关于python - Tkinter-从Spinbox获取值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43849215/