我在这里使用一个名为Combobox\u Autocomplete的类:http://code.activestate.com/recipes/580770-combobox-autocomplete/
它本质上是创建一个条目,带有自动完成功能,基于类实例化时提供给它的列表
在我的例子中,为了简化,假设我必须在表中添加列,如下所示:
Car_model | Color
1 | Red
1 | Blue
1 | Green
2 | Red
2 | Blue
3 | Red
3 | Yellow
我基本上想要的是两个自动完成的盒子,第一个是有车型的,第二个是颜色,颜色会根据选择的车来调整
在更新第二个自动完成块中的第二个列表时,我遇到了一个障碍。下面是我的代码:
class maconomy:
def __init__(self,master):
# Assuming df2 is my table
# Create unique lists of both columns for the autofill features
self.mylist = list(set(self.df2['Car_model'].tolist()))
self.myList2 = list(set(self.df2['Color'].tolist()))
self.combobox_autocomplete1 = Combobox_Autocomplete(master,
self.mylist, highlightthickness=1)
self.combobox_autocomplete1.grid(row = 1, column = 1,padx=10, pady=10)
self.combobox_autocomplete1.bind('<Double-Return>', lambda event,
arg=1: self.update(event, arg))
self.combobox_autocomplete2 = Combobox_Autocomplete(master,
self.myList2, highlightthickness=1)
self.combobox_autocomplete2.grid(row = 2, column = 1,padx=10, pady=10)
self.combobox_autocomplete2.bind('<Double-Return>', lambda event,
arg=2: self.update(event, arg))
# Update method just switches to next combobox
def update(self,event,arg):
if arg == 5:
self.commitButton.focus_set()
else:
eval('self.combobox_autocomplete'+str(arg+1)+'.focus_set()')
我的主要想法是把mylist2变成一处地产。根据车型更新。这就是我所拥有的:
@property
def mylist2(self):
if self.combobox_autocomplete1.get_value() != "":
self.myList2 = df2[df2['Car_model'] ==self.combobox_autocomplete1.get_value()]['Color']
return myList2
else:
return myList2
但是,这不会更新与组合框关联的列表,因为它不会重新初始化组合框。有人知道我怎样才能做到这一点吗
目前没有回答
相关问题 更多 >
编程相关推荐