[英]How to implement my python code into tkinter gui code because nothing is working to stop the program because of multiple while loop runnning
I want to add this code into my tkinter gui code and i tried making it using command in buttons but the problem is, program wont stop running, it just freezes and i alrdy tried things like window.destroy(),exit()
but it just closes the window but exit doesnt quits me out of program until i press the stop button myself我想将此代码添加到我的 tkinter gui 代码中,我尝试使用按钮中的命令来制作它,但问题是,程序不会停止运行,它只是死机,我尝试过
window.destroy(),exit()
类的东西window.destroy(),exit()
但它只是关闭窗口但退出不会让我退出程序,直到我自己按下停止按钮
program code程序代码
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()
Can someone help me add the both code and without freezing the window.... I also tried threading but it didnt worked to stop the program.有人可以帮我添加这两个代码而不冻结窗口....我也尝试过线程,但它没有阻止程序。
tkinter gui code 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()
First your code has never executed recorder()
because you have not assigned any function to the two buttons.首先,您的代码从未执行过
recorder()
因为您没有为这两个按钮分配任何功能。
Second you need to run recorder()
in a thread so that it won't block tkinter mainloop()
.其次,您需要在线程中运行
recorder()
以便它不会阻塞mainloop()
。
In order to stop the recording, you need to find a way to break out from the while loop.为了停止录制,您需要找到一种方法来摆脱 while 循环。 Normally threading.Event object is used in multi-threaded application.
通常threading.Event对象用于多线程应用程序。
Below are the required changes to achieve the goal:以下是实现目标所需的更改:
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)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.