Python 我不确定';我正在正确地运行线程

Python 我不确定';我正在正确地运行线程,python,python-multithreading,Python,Python Multithreading,我正在运行一个线程来不断监视温度,并更新一个全局变量,其他函数调用该变量来相应地执行任务。现在一切都好了,我可以接受。但我想联系一下,看看是否有人有更聪明的方法让这一切继续下去,直到主循环结束。以下是线程的启动方式 thread1 = Thread(target = test) try: thread1.start() except (KeyboardInterrupt, SystemExit): thread1.stop() sys.exit() 我想我可以制作一个小

我正在运行一个线程来不断监视温度,并更新一个全局变量,其他函数调用该变量来相应地执行任务。现在一切都好了,我可以接受。但我想联系一下,看看是否有人有更聪明的方法让这一切继续下去,直到主循环结束。以下是线程的启动方式

thread1 = Thread(target = test)
try:
    thread1.start()
except (KeyboardInterrupt, SystemExit):
    thread1.stop()
    sys.exit()
我想我可以制作一个小脚本让它运行。对不起

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()
正如您将看到的,在关闭窗口后,线程确实结束了,但不是很干净


有什么想法吗?

Tkinter支持协议处理程序

最常用的协议称为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()

基本上,程序会侦听关闭事件,并决定在关闭()函数中关闭窗口时要做什么。

明白了。谢谢Daniel Reyhanian为我指出

协议(“WM_删除_窗口”,关闭时)

事情。以前从没见过

下面是使用以下命令的最后一位:os.\u exit(1)


布鲁,真是浪费时间。祝你好运。os._出口(1)谢谢你@Daniel Reyhanian的帮助!很高兴听到你这么说,显然这不是完全浪费时间。坚持下去!如果你能添加一个关于操作系统的解释就更好了。_exit(1)[看起来它基本上粉碎了所有操作系统,甚至不想寻找错误来报告它们。我还没有在它的专用设备(RaspberryPi)上测试过它但在windows上,我找不到任何证据表明系统中存在任何剩余进程。如果它确实使其处于不确定状态,那么是的,我们有问题,但如果没有,那么这正是我想要的。我将在上午向您更新。另外,请在上午解释什么是不确定状态;)GN!
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()