import tkinter as tk
from tkinter import filedialog
from PIL import ImageGrab
from datetime import datetime
def capture_screenshot(x1, y1, x2, y2, save_path="screenshot.png"):
screenshot = ImageGrab.grab(bbox=(x1, y1, x2, y2))
screenshot.save(save_path)
def get_current_time_in_format():
now = datetime.now()
return now.strftime('%m%d%H%M%S')
def on_capture_button_click():
# 사용자에게 스크린샷을 저장할 위치를 선택하게 합니다.
file_name = f"screenshot_{get_current_time_in_format()}.png"
# path = filedialog.asksaveasfilename(initialfile=file_name, defaultextension=".png", filetypes=[("PNG files", "*.png")])
# 사용자가 취소 버튼을 누른 경우
if not file_name:
return
# 전체 화면 스크린샷을 캡쳐합니다.
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
capture_screenshot(0, 0, screen_width, screen_height, path)
# GUI 생성
root = tk.Tk()
root.title("Screenshot App")
frame = tk.Frame(root, padx=20, pady=20)
frame.pack(padx=10, pady=10)
capture_btn = tk.Button(frame, text="Capture Screenshot", command=on_capture_button_click)
capture_btn.pack()
root.mainloop()