Tkinter:如何放置一个可以随意更新的文本字段?
我希望能够操作一个将在 Tk 窗口中实时显示的整数。
我尝试过:
xp = StringVar()
Label(master, textvariable=xp).pack()
xp.set(0)
然后后来,我尝试过:
xp.set(xp+1)
但它在该行上严重失败,并显示消息:
Exception in Tkinter callback
File "/usr/lib/.../Tkinter.py", line 1413, in __call__
return self.func(*args)
File "rpg.py", line 26, in fight
xp.set(xp+1)
NameError: global name 'xp' is not defined
我希望能够增加“xp”值,并且它实时显示在窗口中。我还希望能够将“xp”作为整数进行操作,使用乘法和指数等。
因此,如果您能指出我做错了什么,我会很高兴。
I want to be able to manipulate an integer that will be displayed in real-time in the Tk window.
I tried:
xp = StringVar()
Label(master, textvariable=xp).pack()
xp.set(0)
and then later, I tried:
xp.set(xp+1)
But it failed badly on that line, with the message:
Exception in Tkinter callback
File "/usr/lib/.../Tkinter.py", line 1413, in __call__
return self.func(*args)
File "rpg.py", line 26, in fight
xp.set(xp+1)
NameError: global name 'xp' is not defined
I want to be able to increment the "xp" value, and it show up in real-time in the window. I also want to be able to manipulate "xp" as an integer, with multiplication and exponents and the such.
So if you can point out what I am doing incorrectly then I would be glad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,第一个代码段中的 xp 变量的范围不包括第二个代码段。这意味着它们确实是不同的变量(碰巧具有非常相似的名称),因此第二个代码段在没有可访问的
StringVar
句柄的情况下运行,这是行不通的。Looks to me like the scope of the
xp
variable in the first snippet does not include the second snippet. This means that they're really different variables (that happen to have a very similar name) and so the second snippet is being run without the handle to theStringVar
accessible, which isn't going to work.