add labels in dropdown menu from tkinter in python

To add labels in a dropdown menu using tkinter in Python, you can use the OptionMenu widget of tkinter. The OptionMenu widget allows you to create a dropdown menu with a list of options to choose from.

Here's an example code snippet that demonstrates how to create a dropdown menu with labels using tkinter in Python:

main.py
import tkinter as tk

root = tk.Tk()

# Create a list of options
options = ["Option 1", "Option 2", "Option 3"]

# Create a StringVar to store the selected option
selected_option = tk.StringVar()

# Set the initial selected option to the first option in the list
selected_option.set(options[0])

# Create a Label widget to display the selected option
label = tk.Label(root, textvariable=selected_option)
label.pack()

# Create an OptionMenu widget with the list of options
option_menu = tk.OptionMenu(root, selected_option, *options)

# Set the background color of the OptionMenu widget
option_menu.config(bg="white")

# Create a Label widget for the dropdown menu
label = tk.Label(root, text="Select an option:")
label.pack()

# Pack the OptionMenu widget
option_menu.pack()

# Start the main event loop
root.mainloop()
821 chars
33 lines

In the example code, we create a list of options and set the initial selected option to the first option in the list. We create a StringVar to store the selected option and use it to update a Label widget that displays the selected option.

We then create an OptionMenu widget with the list of options and pack it in the GUI. Finally, we start the main event loop to display the GUI.

When you run the code, it will display a dropdown menu with labels in the GUI. You can select an option from the dropdown menu, and the selected option will be displayed in the Label widget.

gistlibby LogSnag