- 1 year ago
Dialog box to select option from list in Python on Windows?
To create a dialog box in Python on Windows that allows the user to select an option from a list, you can use the tkinter
library, which provides a GUI toolkit for Python. Here's an example:
pythonimport tkinter as tk
from tkinter import messagebox
def show_dialog():
# Create a tkinter window
window = tk.Tk()
window.withdraw() # Hide the main window
# Define the list of options
options = ['Option 1', 'Option 2', 'Option 3']
# Show the dialog box to select an option
selected_option = messagebox.askquestion('Select Option', 'Please select an option', icon='question',
type=messagebox.YESNO, choices=options)
# Process the selected option
if selected_option:
print('Selected option:', selected_option)
else:
print('No option selected')
# Destroy the tkinter window
window.destroy()
# Call the function to show the dialog box
show_dialog()
In this example, the show_dialog()
function creates a hidden tkinter window and then displays a dialog box using the messagebox.askquestion()
method. The dialog box shows a question with a list of options to select from (Option 1
, Option 2
, Option 3
). The user can select an option by clicking either the "Yes" or "No" buttons.
The selected option is returned as the result of the askquestion()
method, and you can process it as needed.
Make sure to have the tkinter
library installed on your system before running this code. You can install it using pip
if it's not already available.
Note that this example uses the askquestion()
method, which presents a Yes/No dialog. If you want to present a different type of dialog box (e.g., a message box with different buttons or an input box), you can explore other methods available in the messagebox
module of tkinter
to suit your requirements.