打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How to Take a Screenshot using Python

You may use the following template to take a screenshot using Python:

import pyautoguimyScreenshot = pyautogui.screenshot()myScreenshot.save(r'Path to save screenshot\file name.png')

In the next section, I’ll show you the complete steps to take a screenshot using Python. I’ll also include the code to create a GUI to take screenshots:

Steps to Take a Screenshot using Python

Step 1: Install the pyautogui package

To start, you’ll need to install the pyautogui package using the following command (under Windows):

pip install pyautogui

You may check this guide for the instructions to install a package using pip for Windows users.

Step 2: Capture the path to save the screenshot

Next, capture the full path where the screenshot will be saved on your computer.

In my case, I decided to save the screenshot under the following path:

C:\Users\Ron\Desktop\Test\screenshot1.png

Here, the file name is screenshot1, while the file type is png (alternatively, you can set the file type to jpg).

Step 3: Take the screenshot using Python

For the final step, you’ll need to use the template below in order to take the screenshot using Python:

import pyautoguimyScreenshot = pyautogui.screenshot()myScreenshot.save(r'Path to save screenshot\file name.png')

For our example, I applied the following code to take the screenshot (note that you’ll need to change the path to reflect the location where the screenshot will be saved on your computer):

import pyautoguimyScreenshot = pyautogui.screenshot()myScreenshot.save(r'C:\Users\Ron\Desktop\Test\screenshot1.png')

Run the code in Python (adjusted to your path), and the screenshot will saved at your specified location:

Tool to take screenshots using Python

Now, I’ll share with you the code to create a GUI that will allow you to take a screenshot by clicking a button.

As before, you’ll need to adjust the path to reflect the location where the screenshot will be saved on your computer:

import pyautoguiimport tkinter as tkroot= tk.Tk()canvas1 = tk.Canvas(root, width = 300, height = 300)canvas1.pack()def takeScreenshot ():        myScreenshot = pyautogui.screenshot()    myScreenshot.save(r'C:\Users\Ron\Desktop\Test\screenshot2.png')myButton = tk.Button(text='Take Screenshot', command=takeScreenshot, bg='green',fg='white',font= 10)canvas1.create_window(150, 150, window=myButton)root.mainloop()

Run the code, and you’ll see this GUI:

Click on the button, and the screenshot will be saved at your specified location:

So far, you saw how to take a screenshot by specifying the path where the screenshot will be saved. But did you know that you could also take screenshots without specifying the path in the code?

You can definitely do that using the code below:

import pyautoguiimport tkinter as tkfrom tkinter import filedialogroot= tk.Tk()canvas1 = tk.Canvas(root, width = 300, height = 300)canvas1.pack()def takeScreenshot ():        myScreenshot = pyautogui.screenshot()    file_path = filedialog.asksaveasfilename(defaultextension='.png')    myScreenshot.save(file_path)myButton = tk.Button(text='Take Screenshot', command=takeScreenshot, bg='green',fg='white',font= 10)canvas1.create_window(150, 150, window=myButton)root.mainloop()

Run the code, and you’ll get the following GUI:

Click on the button to take the screenshot, and you’ll see a dialogue box that will allow you to choose the location where the screenshot will be saved:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Py之PyAutoGUI:python库之PyAutoGUI的简介、安装、使用方法
cs客户端自动化控制鼠标和键盘操作,python pyautogui
如何迈出 Python 学习第一步?
7行魔性的Python代码成就鼠标键盘自动化,perfect!
使用python编写android截屏脚本
Python 自动化带你轻松赚钱
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服