본문 바로가기
프로그래밍/python

[python] 0.5초마다 지정한 폴더에 스크린샷을 저장하는 gui 프로그램

by 왕초보 개발자 2021. 4. 4.
728x90
import pyautogui
import os
import sys
import tkinter as tk


def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


class Autocapture(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("AutoCapture")  # 타이틀 설정
        # self.call('wm', 'iconphoto', self._w,
        #           tk.PhotoImage(file=resource_path('image/google_images-512.png')))  # 타이틀 아이콘 설정

        width = 640
        height = 240
        x = (self.winfo_screenwidth() / 2) - (width / 2)
        y = (self.winfo_screenheight() / 2) - (height / 2)
        self.geometry("%dx%d+%d+%d" % (width, height, x, y))  # 화면 크기 지정

        self.resizable(False, False)  # 크기 조절 불가 설정

        self._frame = None
        self.switch_frame(StartPage)

    def switch_frame(self, frame_class, Path=None):
        if Path is None:
            new_frame = frame_class(self)
        else:
            new_frame = frame_class(self, Path)

        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack(fill="both")


class StartPage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="폴더 경로 입력", font=('Helvetica', 20, "bold")).pack(side="top", fill="x", pady=20)
        Path = tk.Entry(self, width=50, font=('Helvetica', 14, "bold"))
        Path.pack()

        tk.Button(self, text="Start Recording", font=('Helvetica', 14, "bold"), fg="red",
                  command=(lambda: master.switch_frame(PageOne, Path.get()) if os.path.isdir(
                      Path.get()) else lambda: master.switch_frame(StartPage))).pack(pady=20)


class PageOne(tk.Frame):
    def __init__(self, master, Path):
        tk.Frame.__init__(self, master)
        tk.Frame.configure(self, bg='#D4D4D4')
        tk.Label(self, text="녹화 진행중", font=('Helvetica', 30, "bold"), fg="red").pack(side="top", fill="x", pady=20)

        num = 1
        numStr = str(num).zfill(8)
        self.label = tk.Label(self, text=str(numStr), font=('Helvetica', 30, "bold"), fg="blue")
        self.label.pack()
        self.tiktok(Path, num)

        tk.Button(self, text="Stop Recording", font=('Helvetica', 20, "bold"), fg="red",
                  command=lambda: master.switch_frame(StartPage)).pack(pady=20)

    def tiktok(self, Path, num):
        numStr = str(num).zfill(8)
        self.label.config(text=str(numStr))
        pyautogui.screenshot().save(Path + '\\' + numStr + ".png")
        self.timer = self.after(500, lambda: self.tiktok(Path, num))
        num += 1
        if num > 99999:
            self.quit()


if __name__ == "__main__":
    app = Autocapture()
    app.mainloop()
728x90