帮我写个python程序,有个tkinter的操作界面,有开始和结束两个按钮,当按下开始时,开始录屏,当按下结束时,停止录屏,并把录屏结果保存在根目录下
时间: 2024-03-31 21:37:27 浏览: 69
好的,以下是一个简单的Python程序,其中包含了一个Tkinter操作界面,以及开始和停止录屏的功能:
```python
import tkinter as tk
import threading
import cv2
class ScreenRecorder:
def __init__(self):
self.recording = False
self.filename = "screencast.avi"
self.screen_size = (1920, 1080)
self.fps = 30.0
self.codec = cv2.VideoWriter_fourcc(*"XVID")
self.video_writer = cv2.VideoWriter(self.filename, self.codec, self.fps, self.screen_size)
def start_recording(self):
self.recording = True
threading.Thread(target=self._capture_screen).start()
def stop_recording(self):
self.recording = False
def _capture_screen(self):
while self.recording:
img = cv2.cvtColor(cv2.imread("test.jpg"), cv2.COLOR_BGR2RGB)
img = cv2.resize(img, self.screen_size)
self.video_writer.write(img)
root = tk.Tk()
root.title("Screen Recorder")
recorder = ScreenRecorder()
start_button = tk.Button(root, text="Start Recording", command=recorder.start_recording)
start_button.pack(pady=10)
stop_button = tk.Button(root, text="Stop Recording", command=recorder.stop_recording)
stop_button.pack(pady=10)
root.mainloop()
```
在这个程序中,我们创建了一个名为ScreenRecorder的类,用于控制录屏的行为。在__init__方法中,我们初始化了一些参数,包括文件名、屏幕大小、帧率和编解码器等。在start_recording方法中,我们启动了一个新的线程来捕获屏幕,并将捕获到的帧写入视频文件中。在stop_recording方法中,我们设置recording标志为False,以停止屏幕捕获。
在Tkinter界面中,我们创建了两个按钮,分别用于启动和停止录屏。当按下“Start Recording”按钮时,我们调用ScreenRecorder的start_recording方法来开始录屏。当按下“Stop Recording”按钮时,我们调用ScreenRecorder的stop_recording方法来停止录屏。录屏结果将保存在程序的根目录下,文件名为screencast.avi。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![xmind](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)