|
| 1 | +#Modified by Scott Thomson to make table column width resizable. |
| 2 | + |
| 3 | +from tkinter import * |
| 4 | +#import tkSimpleDialog |
| 5 | + |
| 6 | + |
| 7 | +class MultiListbox(Frame): |
| 8 | + def __init__(self, master, lists): |
| 9 | + Frame.__init__(self, master) |
| 10 | + self.master = master |
| 11 | + self.titles = lists |
| 12 | + self.lists = [] |
| 13 | + self.tablePane = PanedWindow(self,orient=HORIZONTAL) |
| 14 | + self.tablePane.pack(side=LEFT,fill=BOTH,expand=1) |
| 15 | + for l,w in lists: |
| 16 | + |
| 17 | + frame = Frame(self.tablePane) |
| 18 | + frame.pack(side=LEFT, expand=YES, fill=BOTH) |
| 19 | + Label(frame, text=l, borderwidth=1, relief=RAISED).pack(fill=X) |
| 20 | + lb = Listbox(frame, width=w, borderwidth=0, selectborderwidth=0, |
| 21 | + relief=FLAT, exportselection=FALSE) |
| 22 | + lb.pack(expand=YES, fill=BOTH) |
| 23 | + self.lists.append(lb) |
| 24 | + lb.bind('<B1-Motion>', lambda e, s=self: s._select(e.y)) |
| 25 | + lb.bind('<Button-1>', lambda e, s=self: s._select(e.y)) |
| 26 | + lb.bind('<Leave>', lambda e: 'break') |
| 27 | + lb.bind('<B2-Motion>', lambda e, s=self: s._b2motion(e.x, e.y)) |
| 28 | + lb.bind('<Button-2>', lambda e, s=self: s._button2(e.x, e.y)) |
| 29 | + # Scrolling whilst hovering over a list only scrolls one column |
| 30 | + # this attempt at a fix doesn't work. |
| 31 | + lb.bind('<MouseWheel>', self._mousewheel) |
| 32 | + self.tablePane.add(frame) |
| 33 | + frame = Frame(self) |
| 34 | + Label(frame, borderwidth=1, relief=RAISED).pack(fill=X) |
| 35 | + sb = Scrollbar(frame, orient=VERTICAL, command=self._scroll) |
| 36 | + sb.pack(expand=YES, fill=Y) |
| 37 | + #self.tablePane.add(frame,cursor=None,sashcursor=None) |
| 38 | + frame.pack(side=RIGHT,expand=1,fill=Y,anchor=W) |
| 39 | + self.lists[0]['yscrollcommand']=sb.set |
| 40 | + |
| 41 | + def _mousewheel(self,event): |
| 42 | + #print(vars(event)) |
| 43 | + scroll_by = int(-1*(event.delta/120)) |
| 44 | + #print(scroll_by) |
| 45 | + for l in self.lists: |
| 46 | + #l.yview('scroll',scroll_by, 'units') |
| 47 | + l.yview_scroll(scroll_by, "units") |
| 48 | + |
| 49 | + def _select(self, y): |
| 50 | + row = self.lists[0].nearest(y) |
| 51 | + self.selection_clear(0, END) |
| 52 | + self.selection_set(row) |
| 53 | + return 'break' |
| 54 | + |
| 55 | + def _button2(self, x, y): |
| 56 | + for l in self.lists: l.scan_mark(x, y) |
| 57 | + return 'break' |
| 58 | + |
| 59 | + def _b2motion(self, x, y): |
| 60 | + for l in self.lists: l.scan_dragto(x, y) |
| 61 | + return 'break' |
| 62 | + |
| 63 | + def _scroll(self, *args): |
| 64 | + #print(args) |
| 65 | + for l in self.lists: |
| 66 | + #apply(l.yview, args) |
| 67 | + l.yview(*args) |
| 68 | + |
| 69 | + def curselection(self): |
| 70 | + return self.lists[0].curselection() |
| 71 | + |
| 72 | + def delete(self, first, last=None): |
| 73 | + for l in self.lists: |
| 74 | + l.delete(first, last) |
| 75 | + |
| 76 | + def get(self, first, last=None): |
| 77 | + result = [] |
| 78 | + for l in self.lists: |
| 79 | + result.append(l.get(first,last)) |
| 80 | + if last: |
| 81 | + #return apply(map, [None] + result) |
| 82 | + return map([None] + result) |
| 83 | + return result |
| 84 | + |
| 85 | + def index(self, index): |
| 86 | + self.lists[0].index(index) |
| 87 | + |
| 88 | + def insert(self, index, *elements): |
| 89 | + for e in elements: |
| 90 | + i = 0 |
| 91 | + for l in self.lists: |
| 92 | + l.insert(index, e[i]) |
| 93 | + i = i + 1 |
| 94 | + |
| 95 | + def size(self): |
| 96 | + return self.lists[0].size() |
| 97 | + |
| 98 | + def see(self, index): |
| 99 | + for l in self.lists: |
| 100 | + l.see(index) |
| 101 | + |
| 102 | + def selection_anchor(self, index): |
| 103 | + for l in self.lists: |
| 104 | + l.selection_anchor(index) |
| 105 | + |
| 106 | + def selection_clear(self, first, last=None): |
| 107 | + for l in self.lists: |
| 108 | + l.selection_clear(first, last) |
| 109 | + |
| 110 | + def selection_includes(self, index): |
| 111 | + return self.lists[0].selection_includes(index) |
| 112 | + |
| 113 | + def selection_set(self, first, last=None): |
| 114 | + for l in self.lists: |
| 115 | + l.selection_set(first, last) |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + tk = Tk() |
| 119 | + Label(tk, text='MultiListbox').pack() |
| 120 | + mlb = MultiListbox(tk, (('Field Name', 40),('Type', 40), ('Data', 20))) |
| 121 | + for i in range(50): |
| 122 | + mlb.insert(END, ('Field %d' % i,'Binary%d' % i, 'Data%d' % i)) |
| 123 | + mlb.pack(expand=YES,fill=BOTH) |
| 124 | + tk.mainloop() |
0 commit comments