如何在不保存的情况下在 Python Tkinter 窗口中显示图像/屏幕截图?
Tkinter 是一个标准的 Python 库,用于创建和开发基于 GUI 的应用程序。要显示图像,我们使用 PIL 或 Pillow 库。
假设我们想要创建一个应用程序,该应用程序将截取窗口的屏幕截图并在另一个窗口中显示捕获的图像。为了实现这一点,我们可以按照以下步骤操作:
导入所需的库。
创建一个通用的按钮来截取屏幕截图。
定义一个函数来截取屏幕截图。
在给定的函数中,定义我们想要截取屏幕截图的 **坐标** 和 **区域**。
创建一个顶级窗口并在其中定义一个标签图像。
打包小部件并显示输出图像。
示例
# Import the required libraries
from tkinter import *
import pyautogui
from PIL import ImageTk, Image
# Create an instance of tknter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Define a function to take the screenshot
def take_screenshot():
x = 500
y = 500
# Take the screenshot in the given corrds
im1 = pyautogui.screenshot(region=(x, y, 700, 300))
# Create a toplevel window
top = Toplevel(win)
im1 = ImageTk.PhotoImage(im1)
# Add the image in the label widget
image1 = Label(top, image=im1)
image1.image = im1
image1.place(x=0, y=0)
Button(win, text='Take ScreenShot', command=take_screenshot).pack(padx=10, pady=10)
win.mainloop()输出
运行代码时,它将显示一个带有截取屏幕截图按钮的窗口。

现在,单击“截取屏幕截图”按钮,它将捕获大小为 700 像素宽和 300 像素高的屏幕,起始坐标为 (x=500, y=500)。

广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP