Difference between \'askquestion\' and \'askyesno\' in Tkinter

Tkinter's messagebox module provides two similar functions for getting yes/no responses from users: askquestion and askyesno. While both display dialog boxes with "Yes" and "No" buttons, they differ in their return types and optimal use cases.

Understanding askquestion

The askquestion function displays a dialog box and returns a string value based on the user's choice ?

from tkinter import messagebox, Tk

root = Tk()
root.withdraw()  # Hide main window

response = messagebox.askquestion("Save File", "Do you want to save your changes?")
print(f"Response type: {type(response)}")
print(f"Response value: {response}")

if response == "yes":
    print("Saving file...")
else:
    print("Changes discarded.")

root.destroy()
Response type: <class 'str'>
Response value: yes
Saving file...

Understanding askyesno

The askyesno function displays the same dialog but returns a boolean value ?

from tkinter import messagebox, Tk

root = Tk()
root.withdraw()  # Hide main window

response = messagebox.askyesno("Delete File", "Are you sure you want to delete this file?")
print(f"Response type: {type(response)}")
print(f"Response value: {response}")

if response:
    print("File deleted.")
else:
    print("Delete cancelled.")

root.destroy()
Response type: <class 'bool'>
Response value: True
File deleted.

Key Differences

Feature askquestion askyesno
Return Type String ("yes" or "no") Boolean (True or False)
Comparison String comparison needed Direct boolean evaluation
Best For String processing workflows Simple conditional logic

Complete Application Example

Here's a practical example showing both functions in a GUI application ?

import tkinter as tk
from tkinter import messagebox, ttk

class DialogExample:
    def __init__(self, root):
        self.root = root
        self.root.title("Dialog Comparison")
        self.root.geometry("300x150")
        
        # Create buttons
        ttk.Button(root, text="Ask Question (String)", 
                  command=self.use_askquestion).pack(pady=10)
        
        ttk.Button(root, text="Ask Yes/No (Boolean)", 
                  command=self.use_askyesno).pack(pady=10)
        
        self.result_label = ttk.Label(root, text="Click a button to see results")
        self.result_label.pack(pady=20)
    
    def use_askquestion(self):
        response = messagebox.askquestion("Confirm", "Proceed with action?")
        self.result_label.config(text=f"askquestion returned: '{response}' ({type(response).__name__})")
    
    def use_askyesno(self):
        response = messagebox.askyesno("Confirm", "Proceed with action?")
        self.result_label.config(text=f"askyesno returned: {response} ({type(response).__name__})")

# Create and run application
root = tk.Tk()
app = DialogExample(root)
root.mainloop()

When to Use Each Function

Use askquestion when:

  • String processing required When you need to process the response as text

  • Legacy code compatibility Working with existing code that expects string responses

  • Logging purposes When you want to log the exact user response as text

Use askyesno when:

  • Simple boolean logic For straightforward true/false decision making

  • Conditional statements When using direct boolean evaluation in if statements

  • Performance considerations Slightly more efficient as no string comparison is needed

Best Practices

For most applications, askyesno is preferred due to its cleaner boolean logic. Use askquestion only when you specifically need string responses for processing or compatibility reasons.

Conclusion

Both askquestion and askyesno create identical dialog boxes but return different data types. Choose askyesno for simple boolean decisions and askquestion when string processing is required. The boolean approach of askyesno generally leads to cleaner, more readable code.

---
Updated on: 2026-03-27T16:29:13+05:30

713 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements