我正在使用多个列表框(http://code.activestate.com/recipes/52266-multilistbox-tkinter-widget/)的代码
这是为用户创建可供选择的数据列,我希望当用户Listboxselect将记录中的列填充到不同的输入框中时。
我可以在一个函数中使用.bind .delete .insert在一个标准的列表框中工作,但是由于字典中的数据大小不同,它看起来很难看:)
我真的很想继续使用下面的代码,但是我无法让这个函数工作。真的很感激有人能为我指出正确的方向,试着弄清楚这几天,但在周围转圈。
我想解析的代码
def on_selection(event):
line = event.widget.get(event.widget.curselection())
locationent.delete(0, 'end')
cuspnent.delete(0, 'end')
locationent.insert('end', line[0:1])
cuspnent.insert('end', line[1:2])
if __name__ == '__main__':
ttk.Label(leftbottomframe, text='**Select count record from MultiListbox by doubleclick**').pack()
mlb = MultiListbox(leftbottomframe, (('Location', 15),('RRD PN', 15),("Lot/Serial", 15), ('Description', 35), ('Customer PN', 15)))
for i in dic:
mlb.insert(END, (dic[i]['Location'],dic[i]['Item Number'],dic[i]['Lot/Serial'],dic[i]['Description'],dic[i]['Cross_Reference']))
mlb.bind('<<ListboxSelect>>', on_selection)用于多列表框的更大的代码集
data_file= pd.read_excel('CC 180763.xlsx',sheet_name='Sheet1',header=0,converters={'Location':str,'Item Number':str,'Cross_Reference':str,'Description':str})
print (data_file)
dic = data_file.set_index('Reference').transpose().to_dict(orient='dict')# data_file.to_dict(orient='records')
class MultiListbox(Frame):
def __init__(self, master, lists):
Frame.__init__(self, master)
self.lists = []
for l, w in lists:
frame = Frame(self);
frame.pack(side=LEFT, expand=YES, fill=BOTH)
Label(frame, text=l, borderwidth=1, relief=RAISED).pack(fill=X)
lb = Listbox(frame, width=w, borderwidth=0, selectborderwidth=0,
relief=FLAT, exportselection=FALSE)
lb.pack(expand=YES, fill=BOTH)
self.lists.append(lb)
lb.bind('<B1-Motion>', lambda e, s=self: s._select(e.y))
lb.bind('<Button-1>', lambda e, s=self: s._select(e.y))
lb.bind('<Leave>', lambda e: 'break')
lb.bind('<B2-Motion>', lambda e, s=self: s._b2motion(e.x, e.y))
lb.bind('<Button-2>', lambda e, s=self: s._button2(e.x, e.y))
frame = Frame(self);
frame.pack(side=LEFT, fill=Y)
ttk.Label(frame, borderwidth=1, relief=RAISED).pack(fill=X)
sb = Scrollbar(frame, orient=VERTICAL, command=self._scroll)
sb.pack(expand=YES, fill=Y)
self.lists[0]['yscrollcommand'] = sb.set
def _select(self, y):
row = self.lists[0].nearest(y)
self.selection_clear(0, END)
self.selection_set(row)
return 'break'
def _button2(self, x, y):
for l in self.lists: l.scan_mark(x, y)
return 'break'
def _b2motion(self, x, y):
for l in self.lists: l.scan_dragto(x, y)
return 'break'
def _scroll(self, *args):
for l in self.lists:
apply(l.yview, args)
def curselection(self):
return self.lists[0].curselection()
def delete(self, first, last=None):
for l in self.lists:
l.delete(first, last)
def get(self, first, last=None):
result = []
for l in self.lists:
result.append(l.get(first, last))
if last: return apply(map, [None] + result)
return result
def index(self, index):
self.lists[0].index(index)
def insert(self, index, *elements):
for e in elements:
i = 0
for l in self.lists:
l.insert(index, e[i])
i = i + 1
def size(self):
return self.lists[0].size()
def see(self, index):
for l in self.lists:
l.see(index)
def selection_anchor(self, index):
for l in self.lists:
l.selection_anchor(index)
def selection_clear(self, first, last=None):
for l in self.lists:
l.selection_clear(first, last)
def selection_includes(self, index):
return self.lists[0].selection_includes(index)
def selection_set(self, first, last=None):
for l in self.lists:
l.selection_set(first, last)
def on_selection(event):
line = event.widget.get(event.widget.curselection())
locationent.delete(0, 'end')
cuspnent.delete(0, 'end')
locationent.insert('end', line[0:1])
cuspnent.insert('end', line[1:2])
if __name__ == '__main__':
ttk.Label(leftbottomframe, text='**Select count record from MultiListbox by doubleclick**').pack()
mlb = MultiListbox(leftbottomframe, (('Location', 15),('RRD PN', 15),("Lot/Serial", 15), ('Description', 35), ('Customer PN', 15)))
for i in dic:
mlb.insert(END, (dic[i]['Location'],dic[i]['Item Number'],dic[i]['Lot/Serial'],dic[i]['Description'],dic[i]['Cross_Reference']))
mlb.bind('<<ListboxSelect>>', on_selection)
mlb.pack(expand=YES, fill=BOTH)发布于 2019-02-17 21:17:09
问题:当用户列表框选择填充记录中的列时
Frame,这不是您想要的。
#mlb.bind('<>',on_selection)class MultiListbox中添加以下内容:
类MultiListbox(帧):def __init__(self,master,list):Frame.__init__(self,master) self.labels = [] .对于列表中的l,w: self.labels.append(l) lb.bind('<>',self.on_selection)B1...都将防止<<ListboxSelect>>事件。备注:如果您需要这些绑定,则必须链接到self.on_selection。 #lb.bind('<B1-Motion>', lambda e, s=self: s.\_select(e.y)) #lb.bind('<Button-1>', lambda e, s=self: s.\_select(e.y))def on_selection(...成为class MultiListbox的一种方法:
def on_selection(self,event):#获取当前选定的行= event.widget.curselection() #清除前面选定的行,选择当前行self.selection_clear(0,END) self.selection_set(行)#从self.lists #self.lists# Use self.labels as dict key record = {} for列,枚举中的lb (self.lists):record[self.labelscolumn] =lb.get(行、行)打印(‘Selection{}:{}’.format(行,记录))使用:
mlb = MultiListbox(leftbottomframe,
(('Location', 15),('RRD PN', 15),("Lot/Serial", 15),
('Description', 35), ('Customer PN', 15)))
for n in range(3):
mlb.insert(END, ['{}_{}'.format(label, n) for label in mlb.labels])
mlb.pack(expand=YES, fill=BOTH)输出: 选择:{‘Description’:'Description_0','Location':'Location_0','Customer PN':'Customer PN_0','Lot/Serial':'Lot/Serial_0','RRD PN':'RRD PN_0'} Selection1:{'Description':'Description_1','Location':'Location_1','Customer PN_1','Lot/Serial':'Lot/Serial_1','RRD PN':'RRD PN_1'} Selection2:{'Description':'Description_2','Location':'Location_2','Customer PN':'Customer PN_2','Lot/Serial':'Lot/Serial_2','RRD _2‘:'RRD PN_2'}
用Python3.5测试:
https://stackoverflow.com/questions/54735182
复制相似问题