import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
options = tk.StringVar(my_w)
options.set("One") # default selected value
l3 = tk.Label(my_w, text='Select One', width=15 )
l3.grid(row=5,column=1)
om1 =tk.OptionMenu(my_w, options, "HTML","PHP", "MySQL", "Python")
om1.grid(row=5,column=2)
my_w.mainloop() # Keep the window open
We can use elements of an list as options of a OptionMenu. Here we used *my_list
to unpack the list return the elements as options. You can try the command print(*my_list)
to display the elements of any list.
my_list = ["PHP","MySQL","Python","HTML"]
options = tk.StringVar(my_w)
options.set(my_list[1]) # default value
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=5,column=2)
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x250") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
options = tk.StringVar(value='Language')
om1 =tk.OptionMenu(my_w,options,"HTML","PHP","MySQL","Python")
om1.grid(row=5,column=2,padx=100,pady=20)
menu = my_w.nametowidget(om1.menuname)# Get menu widget.
om1.config(font=['Arial',26]) # Set Menu Font
menu.config(font=['Arial',16]) # Set Option Font
my_w.mainloop() # Keep the window open
my_list = ["PHP","MySQL","Python","HTML","JQuery"]
my_list.sort() # sort the list in alphabetical order
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=5)
om1['menu'].insert_separator(2) # add separator after 2nd element
b1 = tk.Button(my_w, text='Show Value',command=lambda: my_show())
Inside the function my_show() we will read the value of Tkinter StringVar() options ( which is the textvariable of OptionMenu opt1 ) and display the same through our Label l2 textvariable str_out.
def my_show():
str_out.set(options.get())
The full code is here.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
options = tk.StringVar(my_w) # get the user selection from OptionMenu om1
options.set("Courses") # default value
l1 = tk.Label(my_w, text='Select One', width=10 )
l1.grid(row=2,column=1)
om1 =tk.OptionMenu(my_w, options, "HTML","PHP", "MySQL", "Python")
om1.grid(row=2,column=2)
b1 = tk.Button(my_w, text='Show Value', command=lambda: my_show() )
b1.grid(row=2,column=3)
str_out=tk.StringVar(my_w)
str_out.set("Output") # default text on Label l2
l2 = tk.Label(my_w, textvariable=str_out, width=10 )
l2.grid(row=2,column=4)
def my_show(): # Update the Label by using StrigVariable str_out
str_out.set(options.get()) # Update the label
my_w.mainloop() # Keep the window open
def my_show(*args):
str_out.set(options.get())
options.trace('w',my_show)
Full code is here
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x120") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
l3 = tk.Label(my_w, text='Select One', width=15 )
l3.grid(row=2,column=1)
my_list = ["PHP","MySQL","Python","HTML"]
options = tk.StringVar(my_w)
options.set(my_list[1]) # default value
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=2)
str_out=tk.StringVar(my_w)
str_out.set("Output")
l2 = tk.Label(my_w, textvariable=str_out, width=10 )
l2.grid(row=2,column=4)
def my_show(*args):
str_out.set(options.get())
options.trace_add('write',my_show) # triggers on change of StringVar options
my_w.mainloop() # Keep the window open
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
# create the dictionary
my_dict1={1: 'Fruits', 2: 'Colors', 3: 'Games', 4: 'Vehicles'}
options = tk.StringVar(my_w) # variable
options.set(my_dict1[1]) # default value
om1 =tk.OptionMenu(my_w, options, *my_dict1.values())
om1.grid(row=2,column=5)
def my_show(*args): # on select function
for i,j in my_dict1.items():
if j==options.get():
print(i)
options.trace('w',my_show)
my_w.mainloop()
Here Dictionary category ( value ) is displayed but on selection ID ( key ) is collected and displayed at console . import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
# create the dictionary
languages = {
"en": "English",
"es": "Spanish",
"fr": "French",
"de": "German",
"it": "Italian"
}
options = tk.StringVar(my_w) # String variable for OptionMenu
options.set(languages['es']) # default value to show
lb1=tk.Label(my_w,text='Language',width=10,bg='yellow')
lb1.grid(row=1,column=1,padx=5,pady=10)
om1 =tk.OptionMenu(my_w, options, *languages.values())
om1.grid(row=1,column=2)
def my_show(*args): # on select function
for i,j in languages.items(): # i= key & j = value
if j==options.get(): # Matches with the selected option
lb1.config(text=i) # Update the Label text with i
options.trace_add('write',my_show) # triggers on change in om1
my_w.mainloop()
print(om1.config().keys()) #om1 is the OptionMenu
AUTHOR
🎥 Join me live on YouTubePassionate about coding and teaching, I love sharing practical programming tutorials on PHP, Python, JavaScript, SQL, and web development. With years of experience, my goal is to make learning simple, engaging, and project-oriented. Whether you're a beginner or an experienced developer, I believe learning by doing is the best way to master coding. Let's explore the world of programming together!