如何从多个 tkinter 选项菜单中获取值
How can I get the values from multiple tkinter Option Menus
美好的一天,
我需要创建一个带有 5 个不同下拉菜单的 window,并且我需要保存 5 个给出的答案。
我设法用 5 个不同的下拉菜单创建了 window。但是,当我 运行 代码时,只保存了最终答案。如何检索所有 5 个选项?
谢谢
# 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)
创建一个列表来存储您在循环中创建的每个 OptionMenu 的 StringVar。
然后对于列表中的每个值,调用 .get() 来获取值。
像这样:
# 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()
美好的一天, 我需要创建一个带有 5 个不同下拉菜单的 window,并且我需要保存 5 个给出的答案。 我设法用 5 个不同的下拉菜单创建了 window。但是,当我 运行 代码时,只保存了最终答案。如何检索所有 5 个选项? 谢谢
# 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)
创建一个列表来存储您在循环中创建的每个 OptionMenu 的 StringVar。 然后对于列表中的每个值,调用 .get() 来获取值。
像这样:
# 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()