如何创建一个 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.