简体   繁体   English

如何将我的 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

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.

相关问题 如何在 python tkinter 中使用 while 循环实现多线程停止按钮 - How to implement multithreading stop button with while loop in python tkinter 我的 tkinter 程序一直冻结/崩溃,我不确定是不是因为代码 - My tkinter program keeps on freezing/crashing and i'm not sure if it's because of the code Python如何在不干扰主代码循环的情况下使用Tkinter GUI - Python how to use Tkinter GUI without interfering the main code loop 如何在Python(tkinter)中使用无限循环停止程序? - How to stop a program with infinity loop in Python (tkinter)? 在下面的 python 代码中,while 循环如何工作? - How is while loop working in the python code below? 我的代码无法正常工作,因为我使用的是在线IDE或是否有错误? - Is my code not working because i'm using an online IDE or is there an error? 为什么我的代码停在secound while循环中? - why my code stop in secound while loop? 为什么当我运行我的 tkinter GUI 代码时,window 出现了,但里面什么都没有? - Why when I run my tkinter GUI code the window shows up but there is nothing inside it? 由于编码错误,python 上的简单摄氏度到华氏度程序无法正常工作。 该程序应通过循环运行 - Simple celsius to fahrenheit program on python not working because of wrong coding. The program should run via loop 如何修复我的错误处理代码? (tkinter 图形用户界面) - how to fix my Error handling code? (tkinter GUI)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM