1. Interface development
#设置主界面 def set_init_window(self): # 去掉tkinter默认的标题 self.tk.title('') # 隐藏默认图标 self.tk.iconbitmap(self.icon_path()) #获取屏幕的宽度 screeWidth = self.tk.winfo_screenwidth() #获取屏幕高度 screeHeight = self.tk.winfo_screenheight() width = int((screeWidth - 500) / 2) height = int((screeHeight - 300) / 2) # 设置主界面的大小和默认位置 self.tk.geometry(f'500x100+{width}+{height}') #添加开始录制按钮,点击之后开启两个线程:一个录屏、一个监听键盘 btn1 = tkinter.Button(self.tk, width=5, height=1, text='开始', command=lambda:[threading.Thread(target=self.video_record).start(),threading.Thread(target=self.start_listen).start()]) btn1.pack() # 设置按钮位置 btn1.place(x=110, y=50, anchor='n') #开启新线程设置录屏范围 btn2 = tkinter.Button(self.tk, width=15, height=1, text='设置录制区域', command=lambda:threading.Thread(target=self.set_range).start()) btn2.pack() btn2.place(x=230, y=50, anchor='n') #生成透明的icon图标 def icon_path(self): ICON = (b'\x00\x00\x01\x00\x01\x00\x10\x10\x00\x00\x01\x00\x08\x00h\x05\x00\x00' b'\x16\x00\x00\x00(\x00\x00\x00\x10\x00\x00\x00 \x00\x00\x00\x01\x00' b'\x08\x00\x00\x00\x00\x00@\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\x01\x00\x00\x00\x01') + b'\x00' * 1282 + b'\xff' * 64 _, ICON_PATH = tempfile.mkstemp() with open(ICON_PATH, 'wb') as icon_file: icon_file.write(ICON) return ICON_PATH
2. Screen recording parameter settings
1. Set the screen recording range
#设置录屏范围 def set_range(self): #self.init_window.withdraw() #隐藏窗口 self.tk.state('icon')#窗口最小化 screeWidth = self.tk.winfo_screenwidth() screeHeight = self.tk.winfo_screenheight() self.newFrame = tkinter.Toplevel(self.tk,width=screeWidth,height=screeHeight,bg='white')#开启新窗口 self.newFrame.attributes('-transparentcolor', 'white') # 使白色为透明色 self.newFrame.overrideredirect(True) # 隐藏导航栏 self.canvas = tkinter.Canvas(self.newFrame,bg='white',bd=0,width=screeWidth,height=screeHeight) self.canvas.bind('<Button-1>', self.onLeftButtonDown)#按下左键 self.canvas.bind('<B1-Motion>', self.onLeftButtonMove)#移动鼠标 self.canvas.bind('<ButtonRelease-1>', self.onLeftButtonUp)#抬起左键 self.canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES) time.sleep(0.3) im = ImageGrab.grab() # 暂存全屏截图 im.save('temp.png') im.close() self.image = tkinter.PhotoImage(file='temp.png') os.remove('temp.png') self.canvas.create_image(screeWidth//2, screeHeight//2, image=self.image)
2. Mouse event monitoring
#按下鼠标 def onLeftButtonDown(self,event): try: self.canvas.delete(self.lastDraw) self.canvas.delete(self.dot1) self.canvas.delete(self.dot2) self.btn3.destroy() except Exception as e: pass self.X = event.x self.Y = event.y self.X2 = 0 self.Y2 = 0 #移动鼠标 def onLeftButtonMove(self,event): try: # 删除刚画完的图形,不然所有画的框都会出现 self.canvas.delete(self.lastDraw) self.canvas.delete(self.dot1) self.canvas.delete(self.dot2) self.btn3.destroy() except Exception as e: pass self.X2 = event.x self.Y2 = event.y self.lastDraw = self.canvas.create_rectangle(self.X, self.Y, event.x, event.y,width=2, outline='pink') #松开鼠标 def onLeftButtonUp(self,event): print("起点", self.X, self.Y) print("终点", self.X2, self.Y2) if self.X2==0 and self.X2==0: return self.width, self.high = self.X2-self.X,self.Y2-self.Y self.region = (self.X, self.Y, self.X2, self.Y2) self.dot1=self.canvas.create_text(self.X, self.Y - 10, text=f'({self.X},{self.Y})', font=("Purisa", 12), fill="pink") self.dot2=self.canvas.create_text(self.X2, self.Y2 + 10, text=f'({self.X2},{self.Y2})', font=("Purisa", 12), fill="pink") # self.newFrame.destroy()#销毁窗口 # self.init_window.deiconify()#显示窗口 # self.tk.state('normal') # 取消窗口最小化 self.btn3 = tkinter.Button(self.canvas, width=15, height=1, text='确定录制区域',bg='pink',fg='#64854c',command=lambda:[self.newFrame.destroy(),self.tk.state('normal')]) self.btn3.pack() self.btn3.place(x=self.X2-20, y=self.Y2+20, anchor='n')
3. Keyboard event monitoring
# 开始监听 def start_listen(self): with keyboard.Listener(on_press=self.on_press) as listener: listener.join() # 监听按键 def on_press(self,key): if key == keyboard.Key.esc: self.flag = True # 改变 return False # 返回False,键盘监听结束!
3. Screen recording operation
def video_record(self): fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D') out = cv2.VideoWriter('output.mp4', fourcc, 14, (self.width, self.high)) # 参数分别为 输出文件名,解码方式,帧数,录像范围 self.count = 1 while (True): img = ImageGrab.grab(self.region) #指定截取坐标(左边X,上边Y,右边X,下边Y) img_np = numpy.array(img) frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB) # ImageGrab获取的颜色为BGR排序,需转换为RGB out.write(frame) self.count += 1 label = tkinter.Label(self.tk, text=f"{int(self.count / 14)}秒") label.pack() label.place(x=320, y=55, anchor='n') # 点击ESC退出 if self.flag == True: tkinter.messagebox.showinfo('提示', '录屏结束') self.flag = False # 改变录屏状态 break out.release() cv2.destroyAllWindows()
The above is the detailed content of How to implement screen recording function in Python. For more information, please follow other related articles on the PHP Chinese website!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools