2017-05-08 54 views
1

我目前無法從spinbox小部件獲取正確的值。我不知道什麼是錯的。我已經尋找解決方案並且已經空了。我究竟做錯了什麼?這裏是我的代碼:Tkinter - 從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() 

回答

1

在你的代碼,a從不更新。相反,讓紡紗器的值,只需使用其.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