帮我写个python程序,有个tkinter的操作界面,有开始和结束两个按钮,当按下开始时,开始录屏,当按下结束时,停止录屏,并把录屏结果保存在根目录下
时间: 2024-03-24 09:40:17 浏览: 64
用python达到录屏效果(录屏小程序)
好的,以下是一个简单的程序,可以实现你的要求:
```python
import tkinter as tk
import cv2
import numpy as np
import datetime
class ScreenRecorder:
def __init__(self):
self.recording = False
self.screen_size = (1920, 1080)
self.fps = 30
self.filename = None
self.video_writer = None
self.video_capture = None
def start_recording(self):
self.recording = True
self.filename = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + ".avi"
fourcc = cv2.VideoWriter_fourcc(*"XVID")
self.video_writer = cv2.VideoWriter(self.filename, fourcc, self.fps, self.screen_size)
self.video_capture = cv2.VideoCapture(0)
def stop_recording(self):
self.recording = False
self.video_capture.release()
self.video_writer.release()
def update(self):
if self.recording:
ret, frame = self.video_capture.read()
if ret:
frame = cv2.resize(frame, self.screen_size)
self.video_writer.write(frame)
self.root.after(1, self.update)
def run(self):
self.root = tk.Tk()
self.root.title("Screen Recorder")
self.root.geometry("300x100")
start_button = tk.Button(self.root, text="Start", command=self.start_recording)
start_button.pack(pady=10)
stop_button = tk.Button(self.root, text="Stop", command=self.stop_recording)
stop_button.pack(pady=10)
self.root.after(1, self.update)
self.root.mainloop()
if __name__ == "__main__":
recorder = ScreenRecorder()
recorder.run()
```
这个程序使用了 OpenCV 库来实现屏幕录制功能,使用 tkinter 库实现了一个简单的操作界面。当按下开始按钮时,程序会开始录屏,并且会将录制结果保存在当前目录下,文件名为当前时间。当按下结束按钮时,程序会停止录屏并保存录制结果。
阅读全文