Tkinter:如何放置一个可以随意更新的文本字段?

发布于 2024-11-28 12:29:22 字数 544 浏览 3 评论 0原文

我希望能够操作一个将在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2024-12-05 12:29:22

在我看来,第一个代码段中的 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 the StringVar accessible, which isn't going to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文