【发布时间】: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