简体   繁体   English

不确定我是否正在正确运行线程

[英]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. Tkinter支持协议处理程序。

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. 最常用的协议称为WM_DELETE_WINDOW,用于定义当用户使用窗口管理器显式关闭窗口时发生的情况。

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(). 基本上,该程序侦听close事件,并决定在functuon on_closing()中杀死该窗口时该怎么做。

Got it. 得到它了。 Thank you Daniel Reyhanian for pointing me toward the 谢谢Daniel Reyhanian向我指出

root.protocol("WM_DELETE_WINDOW", on_closing) root.protocol(“ WM_DELETE_WINDOW”,on_closing)

thing. 事情。 Never seen that before. 以前从未见过。

Here is the final bit using: os._exit(1) 这是最后使用的位: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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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