我希望用户能够从下拉框中选择一个选项(“其他”),然后在一列上显示一个输入框。我希望此功能可用于其他30个下拉菜单。
self.ea_tf = StringVar()
self.ea_tf.set(fixtures[0])
self.e33 = OptionMenu(self.frame1, self.ea_tf, *fixtures, command=self.other_entry(15, "e33", "ea_tf"))
self.e33.grid(row=15, column=5, stick=E+W)
这是函数'other_entry':
def other_entry(self, selection, row, el, var):
if selection == "Other":
self.var = StringVar()
self.el = Entry(self.frame1, textvariable=self.var)
self.el.grid(row=row, column=6)
它出现错误:“app实例没有属性'选择'”。使用其他函数,它会自动给出参数'selection'。如何将选择作为参数之一?
答案 0 :(得分:0)
您的示例中有不正确的缩进。
def other_entry(self, selection, row, el, var):
if selection == "Other":
self.var = StringVar()
self.el = Entry(self.frame1, textvariable=self.var)
self.el.grid(row=row, column=6)
旁边:command
只需要没有()
OptionMenu(..., command=onSelectionChange )
def onSelectionChange(self, selection):
print selection
使用command=self.other_entry(15, "e33", "ea_tf")
运行other_entry(...)
,结果将作为函数名称分配给command
。您的other_entry()
没有return
,因此返回None
(默认值),因此您拥有command=None
。
大多数小部件只发送一个分配给command
的功能。如果您想发送更多参数,则需要lambda
所以你可能需要lambda
函数
OptionMenu(..., command=lambda selection:self.other_entry(selection, 15, "e33", "ea_tf"))
def other_entry(self, selection, row, el, var):
print selection, row, el, var