Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Askquestion Dialog in Tkinter

The askquestion dialog box in tkinter is a simple way to present the user with a question that can be answered with "yes" or "no". It belongs to the tkinter.messagebox module.

Here's a step-by-step tutorial on how to use the askquestion dialog in tkinter:

askquestion Dialog in tkinter Tutorial:

1. Import Required Libraries:

You'll need both tkinter and the messagebox module from tkinter:

import tkinter as tk
from tkinter import messagebox

2. Create a Function to Trigger the Dialog:

def show_question():
    response = messagebox.askquestion("Confirmation", "Do you want to proceed?")
    if response == 'yes':
        label.config(text="You selected 'Yes'")
    else:
        label.config(text="You selected 'No'")

Note that askquestion will return 'yes' or 'no' based on the user's selection.

3. Create the Main Application Window and Widgets:

root = tk.Tk()
root.title("Askquestion Dialog Tutorial")

# Add a button to trigger the dialog
btn = tk.Button(root, text="Show Question", command=show_question)
btn.pack(pady=20)

# Label to display the response
label = tk.Label(root, text="")
label.pack(pady=20)

root.mainloop()

Complete Code:

import tkinter as tk
from tkinter import messagebox

def show_question():
    response = messagebox.askquestion("Confirmation", "Do you want to proceed?")
    if response == 'yes':
        label.config(text="You selected 'Yes'")
    else:
        label.config(text="You selected 'No'")

root = tk.Tk()
root.title("Askquestion Dialog Tutorial")

btn = tk.Button(root, text="Show Question", command=show_question)
btn.pack(pady=20)

label = tk.Label(root, text="")
label.pack(pady=20)

root.mainloop()

Run the code, and when you click the "Show Question" button, you'll get a dialog box with "Yes" and "No" options. The choice will be reflected in a label below the button.

  1. Python Tkinter askquestion example:

    Description: This example demonstrates the usage of the askquestion dialog in Tkinter. The askquestion dialog is used to prompt the user with a yes/no question.

    import tkinter as tk
    from tkinter import messagebox
    
    # Create main window
    root = tk.Tk()
    root.title("askquestion Example")
    
    # Function to display askquestion dialog
    def show_confirmation():
        result = messagebox.askquestion("Confirmation", "Are you sure you want to proceed?")
        if result == "yes":
            messagebox.showinfo("Confirmation", "Proceeding...")
        else:
            messagebox.showinfo("Confirmation", "Cancelled.")
    
    # Create button to trigger askquestion dialog
    confirm_button = tk.Button(root, text="Confirm", command=show_confirmation)
    confirm_button.pack(pady=20)
    
    # Run the main loop
    root.mainloop()