简体   繁体   中英

Not sure I'm running a thread properly

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?

Tkinter supports protocol handlers.

The most commonly used protocol is called WM_DELETE_WINDOW, and is used to define what happens when the user explicitly closes a window using the window manager.

import time
import tkinter
from tkinter import *
from threading import Thread

def on_closing():
    thread1.close()
    root.destroy()

def test():
    global counter
    while timer:
        counter += 1
        var1.set(counter)
        root.update
        time.sleep(1)

root = Tk()

timer = True

global counter
counter = 0
var1 = StringVar()
var1.set(counter)

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)

global thread1 = Thread(target = test)
try:
    thread1.start()
except (KeyboardInterrupt, SystemExit):
    thread1.stop()
    sys.exit()

root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()

Basically, the program listens to the close event and decides what to do as the window is killed in the functuon on_closing().

Got it. Thank you Daniel Reyhanian for pointing me toward the

root.protocol("WM_DELETE_WINDOW", on_closing)

thing. Never seen that before.

Here is the final bit using: os._exit(1)

import os
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 on_closing():
    os._exit(1)

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)
thread1.start()

root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM