Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีสร้างกล่องข้อความแสดงข้อผิดพลาด (Error Message Box) ใน Tkinter


ไลบรารี Tkinter มีฟังก์ชันและเมธอดในตัวมากมายสำหรับพัฒนาส่วนทำงานของแอปพลิเคชัน เราสามารถใช้โมดูล messagebox ใน Tkinter เพื่อสร้างกล่องโต้ตอบป๊อปอัปหลากหลายรูปแบบ คุณสมบัติ 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()

ผลลัพธ์

เมื่อรันโค้ด จะแสดง widget ปุ่มและ label ในหน้าต่าง คลิกปุ่มเพื่อแสดงข้อความข้อผิดพลาด

วิธีสร้างกล่องข้อความแสดงข้อผิดพลาด (Error Message Box) ใน Tkinter