Good day, I need to create a window with 5 different drop down menus and I need to save the 5 given answers. I managed to create the window with the 5 different drop down menus. However, when I run the code, only the final answer is saved. How can I retrieve all the 5 options? THanks
# Import the tkinter module
import tkinter
# Create the default window
root = tkinter.Tk()
root.title("Thruster types")
#root.geometry('700x500')
# Create the list of options
options_list = 'Tunnel', 'Azimuth', 'Pod', 'Shaft Line', 'Cycloid'
#Create an empty list that I will fill with my inputs
Thruster=[None]*5
# Create the optionmenu widget and passing
# the options_list and value_inside to it.
for i in range (0, 5, 1):
# Variable to keep track of the option selected in OptionMenu
value_inside = tkinter.StringVar(root)
# Set the default value of the variable
value_inside.set("Thruster" str(i 1))
question_menu = tkinter.OptionMenu(root, value_inside, *options_list)
question_menu.pack()
def print_answers():
print(value_inside.get()) #Placing the inputs into the list Thruster
return None
# Submit button
# Whenever we click the submit button, our submitted
# option is printed ---Testing purpose
submit_button = tkinter.Button(root, text='Submit', command=print_answers)
submit_button.pack()
root.mainloop()
print(Thruster)
CodePudding user response:
Create a list to store the StringVar for each OptionMenu you create in the loop. Then for each value in the list, call .get() to get the value.
Like this:
# Import the tkinter module
import tkinter
# Create the default window
root = tkinter.Tk()
root.title("Thruster types")
#root.geometry('700x500')
# Create the list of options
options_list = 'Tunnel', 'Azimuth', 'Pod', 'Shaft Line', 'Cycloid'
#Create an empty list that I will fill with my inputs
Thruster=[None]*5
# list of all StringVars for each OptionMenu
values = []
# Create the optionmenu widget and passing
# the options_list and value_inside to it.
for i in range (0, 5, 1):
# Variable to keep track of the option selected in OptionMenu
value_inside = tkinter.StringVar(root)
# Set the default value of the variable
value_inside.set("Thruster" str(i 1))
question_menu = tkinter.OptionMenu(root, value_inside, *options_list)
question_menu.pack()
values.append(value_inside)
def print_answers():
for val in values:
print(val.get()) #Placing the inputs into the list Thruster
return None
# Submit button
# Whenever we click the submit button, our submitted
# option is printed ---Testing purpose
submit_button = tkinter.Button(root, text='Submit', command=print_answers)
submit_button.pack()
root.mainloop()