如何将我的 python 代码实现到 tkinter gui 代码中,因为由于多个 while 循环运行,没有任何东西可以停止程序
How to implement my python code into tkinter gui code because nothing is working to stop the program because of multiple while loop runnning
我想将这段代码添加到我的 tkinter gui 代码中,我尝试使用按钮中的命令来制作它,但问题是,程序不会停止 运行,它只是冻结,我已经尝试过 window.destroy(),exit()
但它只是关闭了 window 但退出并没有让我退出程序,直到我自己按下停止按钮
程序代码
import cv2
import numpy as np
from PIL import ImageGrab
screen_size = (1366, 768)
def recorder():
fourcc = cv2.VideoWriter_fourcc(*"XVID")
fps = 20.0
output = cv2.VideoWriter("output.avi", fourcc, fps, (screen_size))
while True:
img = ImageGrab.grab()
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
output.write(frame)
output.release()
cv2.destroyAllWindows()
谁能帮我添加这两个代码而不冻结 window...我也尝试过线程,但它没有停止程序。
tkinter gui 代码
from tkinter import *
window = Tk()
window.geometry("500x200+460+170")
window.resizable(0, 0)
window.configure(bg='#030818')
Label(window, text="Recording", fg="white",bg="#030818",font=("Helvetica", 23, "bold")).pack()
Button(window, text="Start Recording", bd=0, bg="gray",fg="white",font=("Helvetica", 15, "bold")).place(x=170,y=60)
Button(window, text="Stop Recording", bd=0, bg="gray",fg="white",font=("Helvetica", 15, "bold")).place(x=170,y=110)
window.mainloop()
首先,您的代码从未执行过recorder()
,因为您没有为这两个按钮分配任何功能。
其次你需要在线程中 运行 recorder()
这样它就不会阻塞 tkinter mainloop()
.
为了停止录音,你需要想办法跳出while循环。通常threading.Event对象用于多线程应用程序。
以下是实现目标所需的更改:
import threading
...
recording = threading.Event() # flag used for breaking out the while loop
def recorder():
print("recording started")
fourcc = cv2.VideoWriter_fourcc(*"XVID")
fps = 20.0
output = cv2.VideoWriter("output.avi", fourcc, fps, screen_size)
recording.set() # set the recording flag
while recording.is_set(): # keep running until recording flag is clear
img = ImageGrab.grab().resize(screen_size)
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
output.write(frame)
output.release()
cv2.destroyAllWindows()
print("recording stopped")
def start_recording():
# make sure only one recording thread is running
if not recording.is_set():
# start the recording task in a thread
threading.Thread(target=recorder).start()
def stop_recording():
# stop recording by clearing the flag
recording.clear()
...
Button(window, text="Start Recording", command=start_recording, bd=0, bg="gray",fg="white",font=("Helvetica", 15, "bold")).place(x=170,y=60)
Button(window, text="Stop Recording", command=stop_recording, bd=0, bg="gray",fg="white",font=("Helvetica", 15, "bold")).place(x=170,y=110)
...
我想将这段代码添加到我的 tkinter gui 代码中,我尝试使用按钮中的命令来制作它,但问题是,程序不会停止 运行,它只是冻结,我已经尝试过 window.destroy(),exit()
但它只是关闭了 window 但退出并没有让我退出程序,直到我自己按下停止按钮
程序代码
import cv2
import numpy as np
from PIL import ImageGrab
screen_size = (1366, 768)
def recorder():
fourcc = cv2.VideoWriter_fourcc(*"XVID")
fps = 20.0
output = cv2.VideoWriter("output.avi", fourcc, fps, (screen_size))
while True:
img = ImageGrab.grab()
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
output.write(frame)
output.release()
cv2.destroyAllWindows()
谁能帮我添加这两个代码而不冻结 window...我也尝试过线程,但它没有停止程序。
tkinter gui 代码
from tkinter import *
window = Tk()
window.geometry("500x200+460+170")
window.resizable(0, 0)
window.configure(bg='#030818')
Label(window, text="Recording", fg="white",bg="#030818",font=("Helvetica", 23, "bold")).pack()
Button(window, text="Start Recording", bd=0, bg="gray",fg="white",font=("Helvetica", 15, "bold")).place(x=170,y=60)
Button(window, text="Stop Recording", bd=0, bg="gray",fg="white",font=("Helvetica", 15, "bold")).place(x=170,y=110)
window.mainloop()
首先,您的代码从未执行过recorder()
,因为您没有为这两个按钮分配任何功能。
其次你需要在线程中 运行 recorder()
这样它就不会阻塞 tkinter mainloop()
.
为了停止录音,你需要想办法跳出while循环。通常threading.Event对象用于多线程应用程序。
以下是实现目标所需的更改:
import threading
...
recording = threading.Event() # flag used for breaking out the while loop
def recorder():
print("recording started")
fourcc = cv2.VideoWriter_fourcc(*"XVID")
fps = 20.0
output = cv2.VideoWriter("output.avi", fourcc, fps, screen_size)
recording.set() # set the recording flag
while recording.is_set(): # keep running until recording flag is clear
img = ImageGrab.grab().resize(screen_size)
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
output.write(frame)
output.release()
cv2.destroyAllWindows()
print("recording stopped")
def start_recording():
# make sure only one recording thread is running
if not recording.is_set():
# start the recording task in a thread
threading.Thread(target=recorder).start()
def stop_recording():
# stop recording by clearing the flag
recording.clear()
...
Button(window, text="Start Recording", command=start_recording, bd=0, bg="gray",fg="white",font=("Helvetica", 15, "bold")).place(x=170,y=60)
Button(window, text="Stop Recording", command=stop_recording, bd=0, bg="gray",fg="white",font=("Helvetica", 15, "bold")).place(x=170,y=110)
...