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
-
- import tkinter as tk
- from tkinter import ttk
- from tkinter import Menu
- from tkinter import messagebox as mbox
- err = tk.Tk()
-
- err.title("Python GUI App")
-
- ttk.Label(err, text="Error Messsage BoxApp").grid(column=0,row=0,padx=20,pady=30)
-
- menuBar=Menu(err)
- err.config(menu=menuBar)
-
- def _msgBox():
- mbox.showerror('Python Error Message','Error: You are Clicked ErrorMessage')
-
- infoMenu=Menu(menuBar, tearoff=0)
- infoMenu.add_command(label="Error", command=_msgBox)
- menuBar.add_cascade(label="Message", menu=infoMenu)
-
- 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