Python 获取ttk.Combobox中选定项的索引
我在使用Tkinter时使用组合框时遇到了IF语句的问题。当我想让程序选择速度距离时间时,程序会选择速度加速时间和下拉列表中的所有其他公式。你能帮我做这个吗Python 获取ttk.Combobox中选定项的索引,python,tkinter,combobox,Python,Tkinter,Combobox,我在使用Tkinter时使用组合框时遇到了IF语句的问题。当我想让程序选择速度距离时间时,程序会选择速度加速时间和下拉列表中的所有其他公式。你能帮我做这个吗 dropdown = Combobox(root) dropdown['values']=("Speed, Distance, Time", "Speed, Acceleration, Time", "Gravitational Potential Energy(Ep)", "Kinetic Energy (Ek)", "Elastic P
dropdown = Combobox(root)
dropdown['values']=("Speed, Distance, Time", "Speed, Acceleration, Time", "Gravitational Potential Energy(Ep)", "Kinetic Energy (Ek)", "Elastic Potential Energy (Ee)", "Energy/Work Done, Power, Time", "Energy/Work Done, Force, Distance", "Energy, Voltage, Charge", "Specific Heat Capacity", "Specific Latent Heat", "Efficency", "Pover, Voltage, Current", "Power, Current, Resistance", "Current, Charge, Time", "Voltage, Current, Resistance")
dropdown.pack()
def clicked():
if dropdown['values'][0] == dropdown_formulae[0][0]: # if the user has selected the first option
simpleFormulaMenu(0)
elif dropdown['values'][1] == dropdown_formulae[1][0]:
simpleFormulaMenu(1)
elif dropdown['values'][2] == dropdown_formulae[2][0]:
pass
elif dropdown['values'][3] == dropdown_formulae[3][0]:
pass
elif dropdown['values'][4] == dropdown_formulae[4][0]:
pass
elif dropdown['values'][5] == dropdown_formulae[5][0]:
simpleFormulaMenu(5)
elif dropdown['values'][6] == dropdown_formulae[6][0]:
simpleFormulaMenu(6)
elif dropdown['values'][7] == dropdown_formulae[7][0]:
simpleFormulaMenu(7)
elif dropdown['values'][8] == dropdown_formulae[8][0]:
pass
elif dropdown['values'][9] == dropdown_formulae[9][0]:
pass
elif dropdown['values'][10] == dropdown_formulae[1][0]:
pass
elif dropdown['values'][11]:
simpleFormulaMenu(11)
elif dropdown['values'][12]:
simpleFormulaMenu(12)
elif dropdown['values'][13]:
simpleFormulaMenu(13)
button = tk.Button(text = "Submit", command = clicked)
button.pack()
问题:在ttk.组合框中获取所选项目的索引
核心点:使用
索引
获取所选项目的索引
参考
- 返回值等于x的第一项的seq中基于零的索引
-
方法返回.get()
组合框的当前选定项
-
将虚拟事件
绑定到Combobox小部件
使用Python:3.5-“Tcl版本”:8.6“TkVersion”:8.6测试 问题:在
ttk.组合框中获取所选项目的索引
核心点:使用
索引
获取所选项目的索引
参考
- 返回值等于x的第一项的seq中基于零的索引
-
方法返回.get()
组合框的当前选定项
-
将虚拟事件
绑定到Combobox小部件
用Python测试:3.5-“TclVersion”:8.6“TkVersion”:8.6您应该在
下拉列表中获取所选项目:例如selected=dropdown.get()
,然后在那些if语句中使用selected
,而不是dropdown['values'[…]
。我应该如何在IF语句中使用selected您应该在下拉列表中获取所选项目:例如selected=dropdown.get()
,然后在那些IF语句中使用selected
,而不是在dropdown['values'][…]中使用selected
。我应该如何在IF语句中使用selected
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.combobox = combobox = ttk.Combobox(self)
combobox['values'] = ("Speed, Distance, Time",
"Speed, Acceleration, Time",
"Gravitational Potential Energy(Ep)"
)
combobox.pack()
button = tk.Button(text="Submit", command=self.on_clicked)
button.pack()
def on_clicked(self, *event):
selected = self.combobox.get()
if selected:
idx = self.combobox['values'].index(selected)
print('simpleFormulaMenu({}), selected: {}'.format(idx, selected))
if idx in (2, 3, 4): # pass
pass
else:
# simpleFormulaMenu(idx)
pass
if __name__ == '__main__':
App().mainloop()