Tkinter askyesno

Created with Sketch.

Tkinter askyesno

Summary: in this tutorial, you’ll learn how to use the Tkinter askyesno() function to show a dialog that asks for user confirmation.

Introduction to the Tkinter askyesno() function

Sometimes, you need to ask for user confirmation. For example, if users click the quit button, you want to ask whether they really want to close the application. Or they just accidentally do so:

Tkinter askyesno

To show a dialog that asks for user confirmation, you use the askyesno() function.

The dialog will have a title, a message, and two buttons (yes and no).

When you click the yes button, the function returns True. However, if you click the no button, it returns False.

The following shows the syntax of the askyesno() function:

answer = askyesno(title, message, **options)

Code language: Python (python)

Note that the answer is a Boolean value, either True or False.

Tkinter also has another function called askquestion(), which is similar to the askyesno() function except that it returns a string with a value of 'yes' or 'no':

answer = askquestion(title, message, **options)

Code language: Python (python)

Tkinter askyesno() function example

The following program illustrates how to use the Tkinter askyesno() function:

Tkinter askyesno example

When you click the Quit button, it’ll show a confirmation dialog:

Tkinter askyesno function

If you click the yes button, the application will be closed. Otherwise, it’ll stay.

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import askyesno

# create the root window
root = tk.Tk()
root.title('Tkinter Yes/No Dialog')
root.geometry('300x150')

# click event handler
def confirm():
answer = askyesno(title='confirmation',
message='Are you sure that you want to quit?')
if answer:
root.destroy()

ttk.Button(
root,
text='Ask Yes/No',
command=confirm).pack(expand=True)

# start the app
root.mainloop()

Code language: Python (python)

The following is the same program but use the object-oriented programming approach:

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import askyesno, askquestion

class App(tk.Tk):
def __init__(self):
super().__init__()

self.title('Tkinter Yes/No Dialog')
self.geometry('300x150')

# Quit button
quit_button = ttk.Button(self, text='Quit', command=self.confirm)
quit_button.pack(expand=True)

def confirm(self):
answer = askyesno(title='Confirmation',
message='Are you sure that you want to quit?')
if answer:
self.destroy()

if __name__ == "__main__":
app = App()
app.mainloop()

Code language: Python (python)

Summary

  • Use the Tkinter askyesno() function to show a dialog that asks for user confirmation.
  • The askyesno() function returns True if you click the yes button, otherwise, it returns False.
  • The askquestion() function returns a string with a value of 'yes' or 'no' instead.

Leave a Reply

Your email address will not be published. Required fields are marked *