Create A Message Box That Asks Yes Or No

Intro

In this blog, I am going to create an ask yes or no message box in Python GUI application, when a user clicks "Close" from the action menu, it will ask for the user's choice as - yes or no - in the application screen.

Software requirement

Python 3.5 and IDLE (Python 3.5)

Programming code

  1. #Create ask yes or no message 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. yes_or_no = tk.Tk()  
  7. #Add a Title  
  8. yes_or_no.title("Python GUI App")  
  9. #Label  
  10. ttk.Label(yes_or_no, text="Yes or No action Box").grid(column=0,row=0,padx=20,pady=30)  
  11. #Create a Menu Bar  
  12. menuBar=Menu(yes_or_no)  
  13. yes_or_no.config(menu=menuBar)  
  14. #Display a yes or no Message Box  
  15. def _msgBox():  
  16. mbox.askyesno('Yes or No action Box','Choose the action')  
  17. #Create Message Menu  
  18. infoMenu=Menu(menuBar, tearoff=0)  
  19. infoMenu.add_command(label="Close", command=_msgBox)  
  20. menuBar.add_cascade(label="Message", menu=infoMenu)  
  21. #Calling Main()  
  22. yes_or_no.mainloop()  

About the code

First, I am importing the tkinter modules. 

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

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

Next, create the ask message function and add "yes" and "no" options in displaying command menu.

Finally, I have started the Windows event loop by calling the main loop method. 

Then, let's execute the code.

Output

 
 
 
Ebook Download
View all
Learn
View all