Code: Select all
from tkinter import Tk,Canvas
import time
from threading import Thread
def run_thread(cv):
t = Thread(target=display_clock, name = 'cd',args=(cv,10))
t.daemon = True #on completion the thread will be killed
t.start()
def display_clock(cv,ts):
while True:
cv.create_rectangle((0, 0), (800, 100),fill='black')
cv.create_text((400, 40), fill = 'white' ,text= str(time.time()), font=('Helvetica',40))
time.sleep(ts)
root = Tk()
cv = Canvas(root,width=800,height=100,bg='black')
cv.pack()
run_thread(cv)
root.mainloop()It demonstrates the problem.
Time is displayed every 10 seconds.
I'm using the canvas object in the tkinter mainloop.
I need to have the display calls in its own thread.
The thread is called and the canvas reference is passed
to the display function.
For 30 minutes it works well. The CPU idles at 6% increasing
to 10% at each 10 second call.
After 4 hours we idle at 8% and peak at 100%.
At 10 hours the CPU runs at 100% continuously.
The display gets updated at 15 to 20 second intervals.
Python3 memory use has increased from 16 MB to 18MB.
I cant use this method.
What is going on?
How can I use tkinter and threads?