【问题标题】:How do I set up Tkinter in python to display and update a variable?如何在 python 中设置 Tkinter 以显示和更新变量?
【发布时间】:2016-09-28 00:26:30
【问题描述】:

我试图每 2.5 秒更新一次变量,但不知何故它不会显示在 Tkinter 上。

我编写了除了 Tkinter 相关内容之外的所有代码,因为我在该领域知之甚少。我操纵了从网站获得的代码,该网站提供了使用 Tkinter 的示例。

代码如下:

import threading
from tkinter import *

def onclick():
   pass


its1_c = 0    # first upgrade amount
its2_c = 0    # second upgrade amount
ITS1 = its1_c * 30 # amount of first upgrade owned x multiplier to money
ITS2 = its2_c * 70 # amount of second upgrade owned x multiplier to money

cashflow = 0
balance = 100
def moneygain():
    global cashflow
    global balance
    global text
    text = balance
    cashflow = balance
    threading.Timer(2.5, moneygain).start()
    cashflow = cashflow + 10
    cashflow = cashflow + ITS2
    cashflow = cashflow + ITS1
    balance = cashflow
    print("Balance: " + str(balance))
    text.insert(INSERT, balance)
    root = Tk()
    text = Text(root)
    text.insert(INSERT, balance)
    text.pack()

    text.tag_add("here", "1.0", "1.4")
    text.tag_add("start", "1.8", "1.13")
    text.tag_config("here", background="yellow", foreground="blue")
    text.tag_config("start", background="black", foreground="green")
    root.mainloop()  

moneygain()

当我尝试显示“余额”时,它不会更新。相反,它抛出了这个错误:

Exception in thread Thread-2:
Traceback (most recent call last):

File "D:\Python34\lib\threading.py", line 911, in _bootstrap_inner
    self.run()
  File "D:\Python34\lib\threading.py", line 1177, in run
    self.function(*self.args, **self.kwargs)
  File "D:/Python34/menugui.py", line 27, in moneygain
    text.insert(INSERT, balance)
AttributeError: 'int' object has no attribute 'insert'

如何在 Tkinter 窗口上显示balance

【问题讨论】:

  • "print("Balance" + str(balance))" 是我在测试函数时忘记删除的时候

标签: python variables tkinter display


【解决方案1】:

为了解决您在 tkinter 上更新 balance 变量的问题,这是我的解决方案:

  from Tkinter import *

  root = Tk()
  moneyShown = Label(root, font=('times', 20, 'bold')) #use a Label widget, not Text
  moneyShown.pack(fill=BOTH, expand=1)
  def onclick():
     pass


  its1_c = 0    # first upgrade amount
  its2_c = 0    # second upgrade amount
  ITS1 = its1_c * 30 # amount of first upgrade owned x multiplier to money
  ITS2 = its2_c * 70 # amount of second upgrade owned x multiplier to money

  cashflow = 0
  balance = 100
  def moneygain():
      global cashflow
      global balance
      global text
      text = balance
      cashflow = balance

      cashflow = cashflow + 10
      cashflow = cashflow + ITS2
      cashflow = cashflow + ITS1
      balance = cashflow
      print("Balance: " + str(balance))

      moneyShown.configure(text="Balance: "+str(balance)) #changes the label's text
      moneyShown.after(2500, moneygain) #tkinter's threading (look more into how this works)
  moneygain()
  root.mainloop()

Tkinter 真的不喜欢老式的线程,使用 .after() 的最后一行 moneygain() 的函数效果更好

此外,我还发挥了创造性,将您的 Text 小部件切换为 Label。正如您所说您是该语言的新手,我很确定在这种情况下LabelText 更合适(至少对于这个问题修复!)。

另一个建议:当您要多次调用一个函数(因为我们多次调用moneygain)时,最好不要在这些函数中创建小部件。当我测试你的代码时,它会无限地创建新的 Text 小部件,因为它被一遍又一遍地调用(同样,可能不是你想要的)。

Tkinter 一开始很难学,但是一旦你学会了,它真的很有趣!祝你的项目好运!

【讨论】:

  • 干杯伙伴,你解决了这一切!非常彻底的答案,非常有帮助,比你以往任何时候都多。我的下一个改进是添加按钮来改变 its1_c 和 its2_c 的值
  • @Samuelf80 不客气!如果您正在寻找有关某些小部件语法的快速帮助,请务必查看 effbot.org/tkinterbook。这些人帮助我学习 Tkinter 比我记忆中的要多。
  • 代码中的“tkinter's threading”注释不正确。这与线程无关。当您调用 after 时,您只是在队列中添加一些内容。
猜你喜欢
  • 2017-07-21
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
相关资源
最近更新 更多