Using Warning Message Box In Python GUI Application


In this blog, I am going to create a warning message box in Python GUI application, when a user clicks a  warning item from the message menu, it will display the warning message in the application screen.
Software requirement
Python 3.5 and IDLE (Python 3.5)
Programming code
  1. #Create Warning 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. app = tk.Tk()  
  7. #Add a Title  
  8. app.title("Python GUI App")  
  9. #Label  
  10. ttk.Label(app, text="Warning Messsage Box App").grid(column=0,row=0,padx=20,pady=30)  
  11. #Create a Menu Bar  
  12. menuBar=Menu(app)  
  13. app.config(menu=menuBar)  
  14. #Display a Message Box  
  15. def _msgBox():  
  16.    mbox.showwarning('Python Warning Message','This Python GUI Application Using Warning Box.')  
  17.    #Create warning Message Menu  
  18.    infoMenu=Menu(menuBar, tearoff=0)  
  19.    infoMenu.add_command(label="Warning", command=_msgBox)menuBar.add_cascade(label="Message", menu=infoMenu)  
  20.    #Calling Main()  
  21.    app.mainloop()  
  • 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 warning message function and add the warning message in displaying a command menu.
  • Finally, I have started the windows event loop by calling the mainloop method then I execute the code.