I am running a thread to constantly monitor temperature, and update a global variable that other functions call upon to perform their tasks accordingly. Everything works now, and I could live with it. But I wanted to reach out and see if anyone has a smarter way to keep this going until the mainloop ends. Here is how the thread is started...
thread1 = Thread(target = test)
try:
thread1.start()
except (KeyboardInterrupt, SystemExit):
thread1.stop()
sys.exit()
I guess I could make a small script just to let it run. Sorry...
import time
import tkinter
from tkinter import *
from threading import Thread
root = Tk()
timer = True
global counter
counter = 0
var1 = StringVar()
var1.set(counter)
def test():
global counter
while timer:
counter += 1
var1.set(counter)
root.update
time.sleep(1)
testtext = Label(root,textvariable=var1,font='Consolas 24 bold')
testtext.grid(row=1,column=1,sticky="N,S,E,W",padx=10,pady=0)
testtext2 = Label(root,text="SECONDS",font='Consolas 18 bold')
testtext2.grid(row=2,column=1,sticky="N,S,E,W",padx=10,pady=0)
thread1 = Thread(target = test)
try:
thread1.start()
except (KeyboardInterrupt, SystemExit):
thread1.stop()
sys.exit()
root.mainloop()
As you will see, after you close the window, the thread does end, but not very cleanly.
Any ideas?