Python มีไลบรารี่ของโมดูลและฟังก์ชันมากมายที่ช่วยให้เราสร้างและพัฒนาแอพพลิเคชั่นเด่นได้ Tkinter เป็นไลบรารี Python ที่รู้จักกันดีซึ่งใช้สำหรับสร้างแอปพลิเคชันที่ใช้ GUI หากเราต้องการพัฒนาแอปพลิเคชันที่จับภาพหน้าจอของหน้าต่าง เราก็สามารถใช้ Tkinter เพื่อสร้าง GUI ของแอปพลิเคชันได้อย่างแน่นอน ขั้นตอนต่อไปนี้ของแอปพลิเคชันจะช่วยให้ทราบว่าแอปพลิเคชันของเราทำงานอย่างไร
-
ห้องสมุดที่จำเป็น – หมอน (PIL) สำหรับการประมวลผลภาพ โมดูลเวลาใน Python สำหรับการสุ่มชื่อไฟล์และการประมวลผลยุค
-
สร้างวิดเจ็ตป้ายกำกับในหน้าต่างและเพิ่มปุ่มเพื่อถ่ายภาพหน้าจอ
-
กำหนดฟังก์ชัน ภาพหน้าจอ() ซึ่งจะจับภาพหน้าจอของหน้าต่างและบันทึกไฟล์ในไดเร็กทอรีในเครื่อง
-
เพื่อป้องกันไม่ให้หน้าต่าง Tkinter จับภาพหน้าจอและในภาพ เราสามารถใช้ ถอน() ฟังก์ชันถอนภาพ
ตัวอย่าง
# Import the required libraries
from tkinter import *
import time
from PIL import ImageTk, Image
import pyautogui as pg
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Define a function for taking screenshot
def screenshot():
random = int(time.time())
filename = "C:/Users/Sairam/Documents/" \ + str(random) + ".jpg"
ss = pg.screenshot(filename)
ss.show()
win.deiconify()
def hide_window():
# hiding the tkinter window while taking the screenshot
win.withdraw()
win.after(1000, screenshot)
# Add a Label widget
Label(win, text="Click the Button to Take the Screenshot", font=('Times New Roman', 18, 'bold')).pack(pady=10)
# Create a Button to take the screenshots
button = Button(win, text="Take Screenshot", font=('Aerial 11 bold'), background="#aa7bb1", foreground="white", command=hide_window)
button.pack(pady=20)
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีปุ่มและข้อความป้ายกำกับ

เมื่อเราคลิกปุ่ม ระบบจะจับภาพหน้าจอของหน้าต่างและบันทึกไว้ในไดเร็กทอรีท้องถิ่น
