Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tạo hộp thoại lỗi (error message box) trong Tkinter

Tkinter cung cấp module messagebox để tạo các popup dialog. messagebox.showerror("Title", "Error Message") hiển thị hộp thoại lỗi.

Ví dụ

from tkinter import *
from tkinter import messagebox

win = Tk()
win.geometry("700x350")

def on_click():
    messagebox.showerror('Python Error', 'Error: This is an Error Message!')

Label(win, text="Click the button to show the message", font=('Calibri 15 bold')).pack(pady=20)
Button(win, text="Click Me", command=on_click).pack(pady=20)
win.mainloop()

Kết quả

Cửa sổ có button "Click Me". Click → hiện dialog lỗi tiêu đề "Python Error", nội dung "Error: This is an Error Message!".

Tạo hộp thoại lỗi (error message box) trong Tkinter

Các loại messagebox khác: showinfo, showwarning, askquestion, askokcancel, askyesno, askretrycancel.