Introduction
In this blog, I am going to create a message box in a Python GUI application. When a user clicks the info item from the message menu, it will display a message box on 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
- app = tk.Tk()
-
- app.title("Python GUI App")
-
- ttk.Label(app, text="Messsage Box Application").grid(column=0,row=0,padx=20,pady=30)
-
- menuBar=Menu(app)
- app.config(menu=menuBar)
-
- def _msgBox():
- mbox.showinfo('Python Message Box','This Python GUI Application Using Message box.')
-
- infoMenu=Menu(menuBar, tearoff=0)
- infoMenu.add_command(label="Info", command=_msgBox)
- menuBar.add_cascade(label="Message", menu=infoMenu)
-
- app.mainloop()
About the code
- First, I am importing the tkinter modules.
- Next, I assign a class and variables and give the application title.
- Next, I create a menu bar and add a menu item in the menu bar.
- Next, I create a message function and add a message displaying command in the menu code.
- Finally, I have started the Windows event loop by calling the mainloop method.
- And, I execute the code.
Output