如何在 python 中设置 Tkinter 以显示和更新变量?
How do I set up Tkinter in python to display and update a variable?
我试图每 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()
当我尝试显示 "balance" 时,它没有更新。相反,它抛出了这个错误:
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 window 上显示 balance
?
为了解决让 balance
变量在 tkinter 上更新的问题,我的解决方案是:
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
。正如您所说的您是该语言的新手,我很确定在这种情况下 Label
比 Text
更合适(至少对于此问题修复而言!)。
另一个建议:当你要多次调用一个函数时(就像我们多次调用 moneygain
一样)最好不要 create widgets这些功能。当我测试你的代码时,它会在一遍又一遍地调用时无限地生成新的 Text
小部件(同样,可能不是你想要的)。
Tkinter 刚开始学习起来很棘手,但是一旦你学会了,它真的很有趣!祝你的项目好运!
我试图每 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()
当我尝试显示 "balance" 时,它没有更新。相反,它抛出了这个错误:
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 window 上显示 balance
?
为了解决让 balance
变量在 tkinter 上更新的问题,我的解决方案是:
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
。正如您所说的您是该语言的新手,我很确定在这种情况下 Label
比 Text
更合适(至少对于此问题修复而言!)。
另一个建议:当你要多次调用一个函数时(就像我们多次调用 moneygain
一样)最好不要 create widgets这些功能。当我测试你的代码时,它会在一遍又一遍地调用时无限地生成新的 Text
小部件(同样,可能不是你想要的)。
Tkinter 刚开始学习起来很棘手,但是一旦你学会了,它真的很有趣!祝你的项目好运!