大家好,我正在努力阻止一个帖子,我没有得到任何错误,但我的线程没有停止。我将不胜感激。我有一个按钮,它调用我的功能lev,当我关闭我的按钮时应停止线程。请遵循以下代码部分:
exitFlag = 0
def levt():
print("Executando")
while ((app.frames[Acionamento].var.get()==2) and exitFlag==0):
print("o thread")
t1fvm = time.time() #Tempo final luminária esquerda
n1mv = (t1fvm-t1ivm)*0.6
global levm
levm = levm+n1mv
print(levm)
def lev():
app.frames[Acionamento].esquerdaFrame.vendasFrame.luminaria_esquerdaFramev.label6["text"] = "Luminária A"
global exitFlag
global thread2
thread2 = Thread(target=levt)
if GPIO.input(17):
GPIO.output(17, GPIO.LOW)
app.frames[Acionamento].esquerdaFrame.vendasFrame.luminaria_esquerdaFramev.lev_button["text"]="Desligado"
if thread2.isAlive():
exitFlag = 1
else:
global t1ivm
t1ivm = time.time()
GPIO.output(17, GPIO.HIGH)
app.frames[Acionamento].esquerdaFrame.vendasFrame.luminaria_esquerdaFramev.lev_button["text"]="Ligado"
thread2.start()
答案 0 :(得分:0)
由于始终使用thread2
内的新主题启动lev()
,因此if thread2.isAlive()
将始终评估为False
,而exitFlag
永远不会设置为1因此,永远不会停止正在运行的线程。
将thread2 = Thread(target=levt)
移至thread2.start()
之前的行:
def lev():
...
#thread2 = Thread(target=levt) # moved to line before thread2.start()
if GPIO.input(17):
...
else:
...
thread2 = Thread(target=levt)
thread2.start()