Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
A Yes/No Message Box can be particularly useful when you want the user to confirm a certain action. In Tkinter, this can be achieved using the askyesno
function from the messagebox
module.
Here's a step-by-step tutorial on how to create and use a Yes/No Message Box in Tkinter:
Setting up the basic GUI with Tkinter:
Start with setting up a basic window using tkinter
.
import tkinter as tk from tkinter import messagebox root = tk.Tk() root.title("Yes/No MessageBox in Tkinter") root.geometry("300x200")
Defining a function to show the Yes/No Message Box:
The messagebox.askyesno()
function returns True
if the user clicks "Yes" and False
if they click "No".
def show_yes_no_messagebox(): result = messagebox.askyesno("Confirmation", "Do you want to proceed?") if result: print("User clicked Yes!") else: print("User clicked No!")
Adding a button to display the Yes/No Message Box:
btn = tk.Button(root, text="Show Yes/No MessageBox", command=show_yes_no_messagebox) btn.pack(pady=50)
Run the main loop:
root.mainloop()
Combining the code snippets provided and running the script will yield a tkinter
window with a single button. When you click the button, a Yes/No Message Box will appear, asking the user for confirmation. Depending on the user's choice, the script will print either "User clicked Yes!" or "User clicked No!" to the console.
This kind of confirmation dialog is very useful in scenarios where actions are irreversible or impactful, such as file deletions or sending a message.
How to create a confirmation dialog in Tkinter:
messagebox
to prompt the user for a decision, typically 'Yes' or 'No'.import tkinter as tk from tkinter import messagebox def confirm_action(): result = messagebox.askyesno("Confirmation", "Are you sure you want to proceed?") if result: print("Action confirmed!") else: print("Action canceled.") root = tk.Tk() # Create a confirmation dialog button confirm_button = tk.Button(root, text="Confirm Action", command=confirm_action) confirm_button.pack(pady=20) root.mainloop()
Yes/No prompt in Tkinter messagebox:
askyesno
method in Tkinter's messagebox
to display a Yes/No prompt.import tkinter as tk from tkinter import messagebox def show_yesno_prompt(): result = messagebox.askyesno("Yes/No Prompt", "Do you want to proceed?") if result: print("User clicked Yes.") else: print("User clicked No.") root = tk.Tk() # Create Yes/No prompt button yesno_button = tk.Button(root, text="Show Yes/No Prompt", command=show_yesno_prompt) yesno_button.pack(pady=20) root.mainloop()
Customizing Yes/No options in Tkinter messagebox:
messagebox
to suit specific needs.import tkinter as tk from tkinter import messagebox def custom_yesno_options(): result = messagebox.askyesno("Custom Options", "Do you agree?", icon='info', default='no') if result: print("User agreed.") else: print("User disagreed.") root = tk.Tk() # Create custom Yes/No options button custom_button = tk.Button(root, text="Custom Yes/No Options", command=custom_yesno_options) custom_button.pack(pady=20) root.mainloop()
Creating a decision dialog with Tkinter:
import tkinter as tk from tkinter import messagebox def decision_dialog(): result = messagebox.askyesnocancel("Decision Dialog", "Do you want to save changes?") if result is None: print("User clicked Cancel.") elif result: print("User clicked Yes.") else: print("User clicked No.") root = tk.Tk() # Create decision dialog button decision_button = tk.Button(root, text="Decision Dialog", command=decision_dialog) decision_button.pack(pady=20) root.mainloop()
Tkinter messagebox with Yes and No buttons:
askquestion
method in Tkinter's messagebox
to display a dialog with 'Yes' and 'No' buttons.import tkinter as tk from tkinter import messagebox def show_yesno_question(): result = messagebox.askquestion("Yes/No Question", "Do you want to proceed?") if result == 'yes': print("User clicked Yes.") elif result == 'no': print("User clicked No.") root = tk.Tk() # Create Yes/No question button question_button = tk.Button(root, text="Show Yes/No Question", command=show_yesno_question) question_button.pack(pady=20) root.mainloop()
Handling Yes/No responses in Tkinter:
messagebox
.import tkinter as tk from tkinter import messagebox def handle_yesno_response(): result = messagebox.askyesno("Response Handling", "Is everything okay?") if result: print("User confirmed.") else: print("User denied.") root = tk.Tk() # Handle Yes/No responses button response_button = tk.Button(root, text="Handle Yes/No Response", command=handle_yesno_response) response_button.pack(pady=20) root.mainloop()
Custom icon for Yes/No messagebox in Tkinter:
messagebox
when displaying a Yes/No prompt.import tkinter as tk from tkinter import messagebox def custom_icon_yesno(): result = messagebox.askyesno("Custom Icon", "Do you agree?", icon='question') if result: print("User agreed.") else: print("User disagreed.") root = tk.Tk() # Create custom icon Yes/No button icon_button = tk.Button(root, text="Custom Icon Yes/No", command=custom_icon_yesno) icon_button.pack(pady=20) root.mainloop()
Bind function to Yes/No messagebox in Tkinter:
messagebox
.import tkinter as tk from tkinter import messagebox def on_yes_click(): print("User clicked Yes.") def on_no_click(): print("User clicked No.") def bind_function_to_yesno(): result = messagebox.askyesno("Bind Function", "Do you want to proceed?") if result: on_yes_click() else: on_no_click() root = tk.Tk() # Bind function to Yes/No buttons button bind_button = tk.Button(root, text="Bind Function to Yes/No", command=bind_function_to_yesno) bind_button.pack(pady=20) root.mainloop()
Creating a simple confirmation dialog in Tkinter:
messagebox
with 'Yes' and 'No' options.import tkinter as tk from tkinter import messagebox def simple_confirmation(): result = messagebox.askyesno("Simple Confirmation", "Are you sure?") if result: print("User confirmed.") else: print("User canceled.") root = tk.Tk() # Create a simple confirmation dialog button confirmation_button = tk.Button(root, text="Simple Confirmation", command=simple_confirmation) confirmation_button.pack(pady=20) root.mainloop()