python - Terminate the Thread by using button in Tkinter -
in gui code, tried run loop1 , loop2 @ same time clicking 1 button. thus, used thread
achieve this. tried stop clicking button , failed. after searching on stackoverflow, found out there no directly way kill thread
. here 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 use these 2 buttons 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)
i want loop1 , loop2 can stopped button2. apparently there no terminate
in thread
. used method process
. here code:
from subprocess import call 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()
but program finished after ran it. know there terminate
function in process
. don't know how use it.
a potential solution use event
s. also, rule of thumb when making guis use objects.
from threading import thread,event 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 button calls become 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)
Comments
Post a Comment