如何獲取 Tkinter/Python 中的彈出對話方塊?
Tkinter 是用於建立和開發基於 GUI 應用程式的標準 Python 庫。我們可以在 Tkinter 中建立應用程式,並向其中新增小部件,使應用程式更加互動。
假設我們想在應用程式中顯示一個彈出對話方塊。在這種情況下,我們可以使用 Tkinter 中內建的 messagebox 模組。它允許我們顯示各種對話方塊,例如錯誤、資訊框、確認框等。
示例
在此示例中,我們建立了一個按鈕,單擊該按鈕後將在螢幕上顯示一個彈出訊息。
# Import the required library from tkinter import * from tkinter import ttk from tkinter import messagebox # Create an instance of tkinter frame win=Tk() # Set the geometry win.geometry("700x250") # Define a button to show the popup message box def on_click(): messagebox.showinfo("Message", "Hey folks!") # Add a Label widget Label(win, text="Click the button to open a popup", font=('Georgia 13')) # Create a button to open the popup dialog ttk.Button(win, text="Open Popup", command=on_click).pack(pady=30) win.mainloop()
輸出
執行上述程式碼將顯示一個視窗,其中有一個按鈕用於開啟對話方塊。
單擊按鈕以在螢幕上顯示彈出對話方塊。
廣告