CodeGym /Adesua ahorow /Python SELF TW /操作對話框和彈出視窗

操作對話框和彈出視窗

Python SELF TW
等級 50 , 課堂 1
開放

1. 標準對話框

Tkinter 有內建的模組 messagebox,用於顯示對話框,例如信息提示、警告、錯誤和確認請求。這些對話框方便與使用者互動和創建通知。

引入模組 messagebox


from tkinter import messagebox

使用 messagebox 的主要對話框類型

  1. 信息窗口

    信息窗口告訴使用者重要訊息。使用函數 messagebox.showinfo()

    
    def show_info():
        messagebox.showinfo("信息", "這是一條信息提示")
                
  2. 警告

    警告窗口通知潛在問題或建議注意某些操作。函數 messagebox.showwarning() 用來創建警告窗口。

    
    def show_warning():
        messagebox.showwarning("警告", "這是一條警告消息")
                
  3. 錯誤窗口

    錯誤窗口在出現錯誤時顯示。函數 messagebox.showerror() 用於通知使用者錯誤。

    
    def show_error():
        messagebox.showerror("錯誤", "發生錯誤!")
                
  4. 動作確認請求

    確認請求允許使用者確認或取消操作。函數 messagebox.askyesno() 根據使用者的選擇返回 TrueFalse

    
    def confirm_exit():
        response = messagebox.askyesno("退出", "您確定要退出嗎?")
        if response:
            root.quit()
                

2. 檔案和文件夾選擇

Tkinter 還提供模組 filedialog,允許使用者選擇檔案和文件夾。這對於需要處理檔案的應用程式很有用,例如加載或保存檔案。

引入模組 filedialog


from tkinter import filedialog

filedialog 的主要功能

  1. 打開檔案: 函數 askopenfilename() 允許使用者選擇要打開的檔案。
    
    def open_file():
        file_path = filedialog.askopenfilename(
            title="打開檔案",
            filetypes=[("文字檔案", "*.txt"), ("所有檔案", "*.*")])
        if file_path:
            print("已選擇檔案:", file_path)
                
  2. 保存檔案: 函數 asksaveasfilename() 允許使用者選擇保存檔案的路徑和名稱。
    
    def save_file():
        file_path = filedialog.asksaveasfilename(
            title="保存檔案",
            defaultextension=".txt",
            filetypes=[("文字檔案", "*.txt"), ("所有檔案", "*.*")])
        if file_path:
            print("檔案已保存為:", file_path)
                
  3. 選擇文件夾: 函數 askdirectory() 允許選擇文件夾。
    
    def select_directory():
        directory_path = filedialog.askdirectory(title="選擇文件夾")
        if directory_path:
            print("文件夾已選擇:", directory_path)
                

3. 自定義彈出視窗

除了標準對話框,Tkinter 提供 Toplevel 小工具來創建自定義彈出視窗。這些視窗可以用來創建帶有輸入框、按鈕和其他界面元素的多功能視窗。

使用 Toplevel 創建彈出視窗


import tkinter as tk

# 創建彈出視窗的函數
def open_popup():
    popup = tk.Toplevel(root)
    popup.title("彈出視窗")
    popup.geometry("300x200")

    label = tk.Label(popup, text="輸入你的名字:")
    label.pack(pady=10)

    entry = tk.Entry(popup)
    entry.pack(pady=5)

    # 用於關閉視窗的按鈕
    close_button = tk.Button(popup, text="關閉", command=popup.destroy)
    close_button.pack(pady=10)

# 主窗口
root = tk.Tk()
root.title("主窗口")
root.geometry("400x300")

# 呼叫彈出視窗的按鈕
open_button = tk.Button(root, text="打開彈出視窗", command=open_popup)
open_button.pack(pady=50)

root.mainloop()
    

程式碼解釋

  • tk.Toplevel(root) 創建一個新視窗,顯示在主視窗 root 的上方。
  • 視窗元素: 在彈出視窗中可添加標籤、輸入框和按鈕。
  • "關閉" 按鈕: 函數 popup.destroy 用於關閉彈出視窗。

4. 包含對話框和彈出視窗的完整應用程式

現在,我們將所有討論過的元素組合在一個應用程式中,該應用包括標準對話框、檔案選擇以及自定義彈出視窗。


import tkinter as tk
from tkinter import messagebox, filedialog

# 標準對話框的函數
def show_info():
    messagebox.showinfo("信息", "這是一條信息提示")

def show_warning():
    messagebox.showwarning("警告", "這是一條警告消息")

def show_error():
    messagebox.showerror("錯誤", "發生錯誤!")

def confirm_exit():
    response = messagebox.askyesno("退出", "您確定要退出嗎?")
    if response:
        root.quit()

def open_file():
    file_path = filedialog.askopenfilename(title="打開檔案", filetypes=[("文字檔案", "*.txt"), ("所有檔案", "*.*")])
    if file_path:
        messagebox.showinfo("已選擇檔案", file_path)

def save_file():
    file_path = filedialog.asksaveasfilename(title="保存檔案", defaultextension=".txt", filetypes=[("文字檔案", "*.txt"), ("所有檔案", "*.*")])
    if file_path:
        messagebox.showinfo("檔案已保存為", file_path)

def select_directory():
    directory_path = filedialog.askdirectory(title="選擇文件夾")
    if directory_path:
        messagebox.showinfo("文件夾已選擇", directory_path)

# 自定義彈出視窗的函數
def open_popup():
    popup = tk.Toplevel(root)
    popup.title("彈出視窗")
    popup.geometry("300x200")

    tk.Label(popup, text="輸入你的名字:").pack(pady=10)
    name_entry = tk.Entry(popup)
    name_entry.pack(pady=5)

    tk.Button(popup, text="關閉", command=popup.destroy).pack(pady=10)

# 主窗口
root = tk.Tk()
root.title("包含對話框和彈出視窗的應用程式")
root.geometry("400x400")

# 用於調用不同對話框和彈出視窗的按鈕
tk.Button(root, text="信息", command=show_info).pack(pady=5)
tk.Button(root, text="警告", command=show_warning).pack(pady=5)
tk.Button(root, text="錯誤", command=show_error).pack(pady=5)
tk.Button(root, text="退出確認", command=confirm_exit).pack(pady=5)
tk.Button(root, text="打開檔案", command=open_file).pack(pady=5)
tk.Button(root, text="保存檔案", command=save_file).pack(pady=5)
tk.Button(root, text="選擇文件夾", command=select_directory).pack(pady=5)
tk.Button(root, text="打開彈出視窗", command=open_popup).pack(pady=20)

root.mainloop()
    
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION