问题描述
在我的 GUI 代码中,我尝试通过单击一个按钮同时运行 loop1 和 loop2.因此,我使用 Thread
来实现这一点.但我也试图通过单击另一个按钮来阻止它,但我失败了.在stackoverflow上搜索后,我发现没有直接杀死Thread
的方法.以下是部分代码:
In my GUI code, I tried to run loop1 and loop2 at the same time by clicking one button. Thus, I used Thread
to achieve this. But I also tried to stop it by clicking another button and I failed. After searching on stackoverflow, I found out that there was no directly way to kill Thread
. Here is the part of code:
def loop1():
while True:
call (["raspivid -n -op 150 -w 640 -h 480 -b 2666666.67 -t 5000 -o test.mp4"],shell=True)
call (["raspivid -n -op 150 -w 640 -h 480 -b 2666666.67 -t 5000 -o test1.mp4"],shell=True)
def loop2():
while True:
call (["arecord -D plughw:1 --duration=5 -f cd -vv rectest.wav"],shell=True)
call (["arecord -D plughw:1 --duration=5 -f cd -vv rectest1.wav"],shell=True)
def combine():
Thread(target = loop1).start()
Thread(target = loop2).start()
def stop():
Thread(target = loop1).terminate()
Thread(target = loop2).terminate()
我试着用这两个按钮来控制它.
I tried to use these two buttons to control it.
btn1 = Button(tk, text="Start Recording", width=16, height=5, command=combine)
btn1.grid(row=2,column=0)
btn2 = Button(tk, text="Stop Recording", width=16, height=5, command=stop)
btn2.grid(row=3,column=0)
我希望loop1和loop2可以停止button2.显然,Thread
中没有 terminate
.所以我使用了另一种方法Process
.代码如下:
I want the loop1 and loop2 can be stopped button2. Apparently there is no terminate
in Thread
. So I used another method Process
. Here is the code:
from subprocess import call
from multiprocessing import Process
def loop1():
while True:
call (["raspivid -n -op 150 -w 640 -h 480 -b 2666666.67 -t 5000 -o test.mp4"],shell=True)
call (["raspivid -n -op 150 -w 640 -h 480 -b 2666666.67 -t 5000 -o test1.mp4"],shell=True)
def loop2():
while True:
call (["arecord -D plughw:1 --duration=5 -f cd -vv rectest.wav"],shell=True)
call (["arecord -D plughw:1 --duration=5 -f cd -vv rectest1.wav"],shell=True)
if __name__ == '__main__':
Process(target = loop1).start()
Process(target = loop2).start()
但是这个程序在我运行后立即完成.我知道 Process
中有 terminate
函数.但是不知道怎么用.
But this program finished immediately after I ran it. I know there is terminate
function in Process
. But I don't know how to use it.
推荐答案
一个潜在的解决方案是使用 Event
s.此外,制作 GUI 的一个很好的经验法则是使用对象.
A potential solution would use Event
s. Also, a good rule of thumb when making GUIs is to use objects.
from threading import Thread,Event
from subprocess import call
class Controller(object):
def __init__(self):
self.thread1 = None
self.thread2 = None
self.stop_threads = Event()
def loop1(self):
while not self.stop_threads.is_set():
call (["raspivid -n -op 150 -w 640 -h 480 -b 2666666.67 -t 5000 -o test.mp4"],shell=True)
call (["raspivid -n -op 150 -w 640 -h 480 -b 2666666.67 -t 5000 -o test1.mp4"],shell=True)
def loop2(self):
while not self.stop_threads.is_set():
call (["arecord -D plughw:1 --duration=5 -f cd -vv rectest.wav"],shell=True)
call (["arecord -D plughw:1 --duration=5 -f cd -vv rectest1.wav"],shell=True)
def combine(self):
self.stop_threads.clear()
self.thread1 = Thread(target = self.loop1)
self.thread2 = Thread(target = self.loop2)
self.thread1.start()
self.thread2.start()
def stop(self):
self.stop_threads.set()
self.thread1.join()
self.thread2.join()
self.thread1 = None
self.thread2 = None
这样你的按钮调用就会变成这样:
This way your button calls would become something like:
control = Controller()
btn1 = Button(tk, text="Start Recording", width=16, height=5, command=control.combine)
btn1.grid(row=2,column=0)
btn2 = Button(tk, text="Stop Recording", width=16, height=5, command=control.stop)
btn2.grid(row=3,column=0)
这篇关于使用 Tkinter 中的按钮终止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!