gpt4 book ai didi

python - Tkinter - 从 spinbox 获取值

转载 作者:太空宇宙 更新时间:2023-11-03 15:16:31 28 4
gpt4 key购买 nike

我目前无法从旋转框小部件获取正确的值。我不知道出了什么问题。我已经寻找解决方案但一无所获。我究竟做错了什么?这是我的代码:

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 永远不会更新。相反,要获取旋转框值,只需使用其 .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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com