Python - 多列表框到条目框

问题描述 投票:0回答:1

我正在使用多列表框的代码(http://code.activestate.com/recipes/52266-multilistbox-tkinter-widget/

这是为用户选择创建数据列,我希望当用户Listbox选择将记录中的列填充到不同的Entry框中时。

我可以在函数中使用.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)
python tkinter
1个回答
1
投票

问题:当用户Listbox选择记录中的列得到填充时

  1. 删除它,因为它绑定到Frame,这不是你想要的。 #mlb.bind('<<ListboxSelect>>', on_selection)
  2. 将以下内容添加到class MultiListboxclass MultiListbox(Frame): def __init__(self, master, lists): Frame.__init__(self, master) self.labels = [] ... for l, w in lists: self.labels.append(l) lb.bind('<<ListboxSelect>>', self.on_selection) ...
  3. 删除以下,因为绑定任何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))
  4. 让qazxsw poi成为qazxsw poi的一种方法: qazxsw poi

用法:

def on_selection(...

输出:

class MultiListbox

用Python测试:3.5

© www.soinside.com 2019 - 2024. All rights reserved.