Create A Message Box That Asks Retry Or Cancel In Python GUI Application

Intro

In this blog, I am going to create a message box that asks retry or cancel, in Python GUI application. When a user clicks “Exit” from action menu, it will ask for the user’s choice as - retry or cancel - in the application screen.

Software requirement

Python 3.5 and IDLE (Python 3.5)

Programming code

  1. #Create retry or cancel action 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="Retry or Cancel action Box").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 retry or cancel Message Box  
  15. def _msgBox():  
  16. mbox.askretrycancel ('Retry or Cancel action Box','Choose the action')  
  17. #Create Message Menu  
  18. msgMenu=Menu(menuBar, tearoff=0)  
  19. msgMenu.add_command(label="Exit", command=_msgBox)  
  20. menuBar.add_cascade(label="Action", menu=msgMenu)  
  21. #Calling Main()  
  22. app.mainloop()  

About the code

First, I am importing the tkinter modules.

Next, assign a class and variables and give application title.

Next, create a menu bar and add a menu item in the menu bar.

Next, create ask message function and add “Retry” or “Cancel” options in displaying command menu.

Finally, I have started the windows event loop by calling the mainloop method.

Then, let’s execute the code.

Output

 

Ebook Download
View all
Learn
View all