Reputation: 119
I am trying to update a variable every 2.5 seconds, but somehow it will not display on Tkinter.
I wrote all of the code except Tkinter related things as I have very little knowledge in that area. I manipulated code I got from a website giving an example for using Tkinter.
Here is the code:
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()
When I try to display "balance" it does not update. Instead it throws out this error:
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'
How can I display balance
on the Tkinter window?
Upvotes: 0
Views: 788
Reputation: 1005
To solve your problem of getting your balance
variable to update on tkinter here is my solution:
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 really doesnt like old fashioned threading, and works much better using the function .after()
as used on the last line of moneygain()
Also I took creative liberty and switched your Text
widget to a Label
. As you said you are new to the language, I'm pretty sure a Label
is much more appropriate than a Text
in this situation (at least for this problem fix!).
Another suggestion: when you are going to call a function multiple times (as we are calling moneygain
multiple times) its a good practice not to create widgets in these functions. When I was testing your code, it infinitely made new Text
widgets as it was called over and over (again, probably not what you wanted).
Tkinter is very tricky to learn at first but once you learn it, it is really fun! Good luck on your project!
Upvotes: 1