美好的一天,我需要創建一個帶有 5 個不同下拉選單的視窗,我需要保存 5 個給出的答案。我設法用 5 個不同的下拉選單創建了視窗。但是,當我運行代碼時,只保存了最終答案。如何檢索所有 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)
uj5u.com熱心網友回復:
創建一個串列來存盤您在回圈中創建的每個 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323738.html
下一篇:如何停止列印機功能?
