如何建立一個 Tkinter 錯誤訊息框?


Tkinter 庫具有許多內建方法,可用於實現應用程式的功能部分。我們可以在 Tkinter 中使用messagebox模組建立各種彈出對話方塊。messagebox屬性具有不同型別的內建彈出視窗,使用者可以在其應用程式中使用它們。

如果你需要在應用程式中顯示錯誤messagebox,可以使用showerror("Title", "Error Message")方法。可以用messagebox本身來呼叫此方法。

例項

# Import the required libraries
from tkinter import *
from tkinter import messagebox

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Define a function to show the error message
def on_click():
   messagebox.showerror('Python Error', 'Error: This is an Error Message!')

# Create a label widget
label = Label(win, text="Click the button to show the message ",
font=('Calibri 15 bold'))
label.pack(pady=20)


# Create a button to delete the button
b = Button(win, text="Click Me", command=on_click)
b.pack(pady=20)

win.mainloop()

輸出

當你執行以上程式碼時,它將在視窗中顯示一個按鈕小部件和一個標籤。單擊按鈕可顯示錯誤訊息。

更新時間:06-Aug-2021

18K+ 瀏覽次數

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.