I am adding drop downs dynamically based on user input. The user will input a title. Each word in the title has a list of synonyms associated with them. Tkinter will add drop downs of synonyms dynamically based on the user input. However I cannot individually access the value of each drop down.

The lists variable is a list of lists containing the synonyms for each word in the user input.

def ok():
    print ("value is:" + variable.get())

 i = 0
    while i < len(lists):

        OPTIONS = lists[i] 

        OPTIONS = OPTIONS.split(',')

        variable = StringVar(master)

        dropdown = OptionMenu(master, variable, *OPTIONS)
        dropdown.pack(side=LEFT, fill=X, expand=YES)

        button = Button(master, text="Swap", command=ok)
        button.pack(side=LEFT, expand=YES)

        i = i+1

Is there a way I can access the value set from my drop downs?

1 Answers

1
furas On Best Solutions

Using command= in OptionMenu you can assign function which will get selected value when you change selection.

import tkinter as tk

#--- functions ---

def on_select(text):
    print('text:', text)
    print('---')

#--- main ---

data = ['a,b,c', 'x,y,z']

root = tk.Tk()

for i, options in enumerate(data):
    options = options.split(',')
    op = tk.OptionMenu(root, tk.StringVar(), *options, command=on_select)
    op.pack()

root.mainloop()

command= in Button expected function which doesn't get parameters - ie. def on_select(): but command= in OptionMenu expects function which gets single parameter - ie. def on_select(text):

This code doesn't need variable = tk.StringVar()


So you have selected value but you don't know in which OptionMenu it was selected - in first or second. Using lambda you can assign function which will get more parameters and you can put number as second parameter.

import tkinter as tk

#--- functions ---

def on_select(text, number):
    print('text:', text)
    print('number:', number)
    print('data[number]:', data[number])
    print('---')

#--- main ---

data = ['a,b,c', 'x,y,z']

root = tk.Tk()

for i, options in enumerate(data):
    options = options.split(',')
    op = tk.OptionMenu(root, tk.StringVar(), *options, command=lambda text, number=i:on_select(text, number))
    op.pack()

root.mainloop()

So now first OptionMenu runs on_select(text, 0) and second runs on_select(text, 1) so function knows which OptionMenu was selected and it can get correct values from data (or do other things)


You can also use StringVar() to get selected values. if you need all selected values then you have to create separated StringVar for every OptionMenu and keep on list. When you click button then you can use for loop with list of variables to get all selected values.

import tkinter as tk

#--- functions ---

def on_click():
    for number, var in enumerate(all_variables):
        print(number, '| selected:', var.get(), '| all:', data[number])

#--- main ---

data = ['a,b,c', 'x,y,z']

root = tk.Tk()

all_variables = []

for options in data:
    options = options.split(',')
    var = tk.StringVar()
    all_variables.append(var)
    op = tk.OptionMenu(root, var, *options)
    op.pack()

b = tk.Button(root, text='OK', command=on_click)
b.pack()

root.mainloop()