Using Error Message Box In Python GUI Application

Intro

In this blog, I am going to create an error message in a Python GUI application. When a user clicks an error item from the message menu, it will display the error message in the application screen.

Software requirement

Python 3.5 and IDLE (Python 3.5)

Programming code

  1. #Create Error Box in Python GUI Application  
  2. import tkinter as tk  
  3. from tkinter import ttk  
  4. from tkinter import Menu  
  5. from tkinter import messagebox as mbox  
  6. err = tk.Tk()  
  7. #Add a Title  
  8. err.title("Python GUI App")  
  9. #Label  
  10. ttk.Label(err, text="Error Messsage BoxApp").grid(column=0,row=0,padx=20,pady=30)  
  11. #Create a Menu Bar  
  12. menuBar=Menu(err)  
  13. err.config(menu=menuBar)  
  14. #Display a Error Message Box  
  15. def _msgBox():  
  16. mbox.showerror('Python Error Message','Error: You are Clicked ErrorMessage')  
  17. #Create Error Message Menu  
  18. infoMenu=Menu(menuBar, tearoff=0)  
  19. infoMenu.add_command(label="Error", command=_msgBox)  
  20. menuBar.add_cascade(label="Message", menu=infoMenu)  
  21. #Calling Main()  
  22. err.mainloop()  

About the code

  • First, I am importing the tkinter modules.
  • Next, I assign a class and variables and give application title.
  • Next, I create a menu bar and add an item in the menu bar.
  • Next, I create an error message function and add the error message in the displaying command menu.
  • Finally, I have started the Windows event loop by calling the mainloop method, then execute the code.

Output