Python Tkinter IntVar 未更新 [英] Python Tkinter IntVar Not Updating
问题描述
我有两个函数是 Python 3.4.1 Tkinter GUI 的一部分.
I have two functions that are part of a Python 3.4.1 Tkinter GUI.
def jumpto():
global jump
jump = Tk()
jump.wm_title("Jump")
jump.focus_force()
label = Label(jump, text = "Enter digit to jump:").pack()
global jumptext
jumptext = IntVar()
jumpentry = Entry(jump, textvariable = jumptext)
jumpentry.pack()
jump.bind("<Return>", close)
def close(self):
global jumptext
global jump
print(jumptext.get())
while digit < jumptext.get(): #digit is an integer that increases in the unrelated area below
#Do something completely unrelated
jump.destroy()
jButton = Button(master, text = "JUMP", command = jumpto).pack() #master is the main Tk window
但是,当我运行代码时,close
中的 jumptext.get()
仍然为 0,尽管在使用 jumpentry 创建的输入框中输入了一些内容代码>.有什么方法可以使输入的信息实际更新到
jumptext.get()
?
However, when I run the code, jumptext.get()
within close
remains 0, despite something being typed in to the entry box created with jumpentry
. Any way to make it so that the information entered actually updates to jumptext.get()
?
另外,如果有人能向我解释为什么必须在 close
中输入 self
,那将是一个奖励.
Also, if anyone could explain to me why self
must be entered within close
, that would be a bonus.
提前致谢!
推荐答案
问题是您正在创建 Tk()
的两个实例,这会导致奇怪的行为.jump
窗口应该是 Toplevel
窗口,如果您希望在 Tk()
窗口旁边有另一个窗口,则使用该窗口.
The thing is that you're making two instances of Tk()
, which leads to strange behavior. The jump
window should be a Toplevel
window, which is the window to use if you want another window next to your Tk()
window.
close
需要接受一个参数,因为它被一个 bind
调用,它总是传递一个 event 对象,其中包含有关触发 close
函数的事件的各种信息(例如鼠标位置和键用于触发事件).所以称它为 event
而不是 self
实际上会更正确.self
是在包含类属性的类中使用的变量.
close
needs to accept an argument because it is called by a bind
which always passes an event object which contains all kinds of information about the event that triggered the close
function (things like mouse position and the key that was used to trigger the event). So calling it event
instead of self
would actually be more correct. self
is a variable used in classes which contains class attributes.
这篇关于Python Tkinter IntVar 未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!