GUI介面程式碼(家)

不上火星不改名發表於2024-03-31
import tkinter as tk
from tkinter import filedialog, ttk
from PIL import Image, ImageTk

class PhotoSketchGUI:
    def __init__(self, root):
        self.root = root
        self.root.title("建築效果圖轉彩色手繪")
        self.root.configure(bg='black')

        # 使視窗最大化
        self.root.state('zoomed')

        self.setup_ui()

    def setup_ui(self):
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()

        self.canvas_width = screen_width * 0.4
        self.canvas_height = screen_height * 0.8
        self.left_canvas_x = screen_width * 0.25 - self.canvas_width / 2
        self.right_canvas_x = screen_width * 0.75 - self.canvas_width / 2

        self.intro_label1 = tk.Label(self.root, text="建築效果圖轉彩色手繪", bg='black', fg='white', font=('微軟雅黑', 18, 'bold'))
        self.intro_label1.place(x=self.left_canvas_x, y=10)

        self.intro_label2 = tk.Label(self.root, text="使用介紹: 請在左側框內上傳一張建築渲染圖", bg='black', fg='white', font=('微軟雅黑', 10))
        self.intro_label2.place(x=self.left_canvas_x, y=45)

        self.left_frame = tk.Canvas(self.root, width=self.canvas_width, height=self.canvas_height, bg='black', highlightthickness=0)
        self.left_frame.place(x=self.left_canvas_x, rely=0.5, anchor='w')
        self.left_frame.create_rectangle(2, 2, self.canvas_width-2, self.canvas_height-2, outline='white', dash=(5, 5))

        self.upload_btn = tk.Button(self.root, text="點選選擇", command=self.upload_image, bg='darkgrey', fg='white', font=('微軟雅黑', 12))
        self.upload_btn.place(x=self.left_canvas_x + self.canvas_width / 2, rely=0.5, anchor='center')

        self.right_frame = tk.Canvas(self.root, width=self.canvas_width, height=self.canvas_height, bg='black', highlightthickness=0)
        self.right_frame.place(x=self.right_canvas_x, rely=0.5, anchor='w')
        self.right_frame.create_rectangle(2, 2, self.canvas_width-2, self.canvas_height-2, outline='white', dash=(5, 5))

        self.style = ttk.Style(self.root)
        self.style.theme_use('default')
        self.style.configure("White.Horizontal.TProgressbar", background='white')

        progress_width = self.right_canvas_x + self.canvas_width - self.left_canvas_x
        self.progress = ttk.Progressbar(self.root, style="White.Horizontal.TProgressbar", orient=tk.HORIZONTAL, length=progress_width, mode='determinate')
        self.progress['value'] = 0

        self.root.after(100, self.set_progress_position, self.left_canvas_x, self.canvas_height, 15)
        self.root.after(100, self.add_save_path_button)

    def add_save_path_button(self):
        button_width = 120
        button_height = 30
        button_x = self.left_canvas_x + self.canvas_width - button_width
        button_y = self.left_frame.winfo_y() - 14 - button_height  # 將按鈕再向上移動4個畫素

        self.save_path_btn = tk.Button(self.root, text="儲存圖片路徑", command=self.save_image_path, bg='darkgrey', fg='white', font=('微軟雅黑', 12))
        self.save_path_btn.place(x=button_x, y=button_y, width=button_width, height=button_height)

    def set_progress_position(self, left_canvas_x, canvas_height, gap):
        progress_y = self.left_frame.winfo_y() + canvas_height + gap
        progress_width = self.right_frame.winfo_x() + self.right_frame.winfo_width() - left_canvas_x
        self.progress.place(x=left_canvas_x, y=progress_y, width=progress_width, height=20)

    def update_progress(self, value=0):
        new_value = value + 5
        if new_value > 100:
            self.progress['value'] = 100
        else:
            self.progress['value'] = new_value
            self.root.after(100, self.update_progress, new_value)

    def upload_image(self):
        filepath = filedialog.askopenfilename()
        if filepath:
            img = Image.open(filepath)
            img.thumbnail((self.left_frame.winfo_width(), self.left_frame.winfo_height()))
            img = ImageTk.PhotoImage(img)

            if hasattr(self, 'image_label'):
                self.image_label.configure(image=img)
                self.image_label.image = img
                self.image_label.place(x=self.left_frame.winfo_x(), rely=0.5, anchor='w')
            else:
                self.image_label = tk.Label(self.root, image=img, bg='black')
                self.image_label.image = img
                self.image_label.place(x=self.left_frame.winfo_x(), rely=0.5, anchor='w')

            self.upload_btn.place_forget()

            self.style.configure("Green.Horizontal.TProgressbar", background='green')
            self.progress.configure(style="Green.Horizontal.TProgressbar")
            self.update_progress()

    def save_image_path(self):
        folder_path = filedialog.askdirectory()
        if folder_path:
            print("Selected folder:", folder_path)  # 在後臺列印選擇的資料夾路徑
            # 這裡你可以按照需要處理或儲存路徑

if __name__ == "__main__":
    root = tk.Tk()
    app = PhotoSketchGUI(root)
    root.mainloop()

相關文章