Создаёт многоколоночный список с заголовками. Колонки настраиваемой ширины. Пример встроен в скрипт.
1 from Tkinter import *
2 import time
3
4 def fill():
5 for i in range(1000):
6 time.sleep(0.1)
7 mlb.insert(END, ('Important Message: %d' % i, 'John Doe', '10/10/%04d' % (1900+i)))
8 mlb.pack(expand=YES,fill=BOTH)
9 tk.update_idletasks()
10
11 class MultiListbox(Frame):
12 def __init__(self, master, lists):
13 Frame.__init__(self, master)
14 self.lists = []
15 for l,w in lists:
16 frame = Frame(self); frame.pack(side=LEFT, expand=YES, fill=BOTH)
17 Label(frame, text=l, borderwidth=1, relief=RAISED).pack(fill=X)
18 lb = Listbox(frame, width=w, borderwidth=0, selectborderwidth=0,
19 relief=FLAT, exportselection=FALSE)
20 lb.pack(expand=YES, fill=BOTH)
21 self.lists.append(lb)
22 lb.bind('<B1-Motion>', lambda e, s=self: s._select(e.y))
23 lb.bind('<Button-1>', lambda e, s=self: s._select(e.y))
24 lb.bind('<Leave>', lambda e: 'break')
25 lb.bind('<B2-Motion>', lambda e, s=self: s._b2motion(e.x, e.y))
26 lb.bind('<Button-2>', lambda e, s=self: s._button2(e.x, e.y))
27 frame = Frame(self); frame.pack(side=LEFT, fill=Y)
28 Label(frame, borderwidth=1, relief=RAISED).pack(fill=X)
29 sb = Scrollbar(frame, orient=VERTICAL, command=self._scroll)
30 sb.pack(expand=YES, fill=Y)
31 self.lists[0]['yscrollcommand']=sb.set
32
33 def _select(self, y):
34 row = self.lists[0].nearest(y)
35 self.selection_clear(0, END)
36 self.selection_set(row)
37 return 'break'
38
39 def _button2(self, x, y):
40 for l in self.lists: l.scan_mark(x, y)
41 return 'break'
42
43 def _b2motion(self, x, y):
44 for l in self.lists: l.scan_dragto(x, y)
45 return 'break'
46
47 def _scroll(self, *args):
48 for l in self.lists:
49 apply(l.yview, args)
50
51 def curselection(self):
52 return self.lists[0].curselection()
53
54 def delete(self, first, last=None):
55 for l in self.lists:
56 l.delete(first, last)
57
58 def get(self, first, last=None):
59 result = []
60 for l in self.lists:
61 result.append(l.get(first,last))
62 if last: return apply(map, [None] + result)
63 return result
64
65 def index(self, index):
66 self.lists[0].index(index)
67
68 def insert(self, index, *elements):
69 for e in elements:
70 i = 0
71 for l in self.lists:
72 l.insert(index, e[i])
73 i = i + 1
74 for l in self.lists:
75 l.see(END)
76
77 def size(self):
78 return self.lists[0].size()
79
80 def see(self, index):
81 for l in self.lists:
82 l.see(index)
83
84 def selection_anchor(self, index):
85 for l in self.lists:
86 l.selection_anchor(index)
87
88 def selection_clear(self, first, last=None):
89 for l in self.lists:
90 l.selection_clear(first, last)
91
92 def selection_includes(self, index):
93 return self.lists[0].selection_includes(index)
94
95 def selection_set(self, first, last=None):
96 for l in self.lists:
97 l.selection_set(first, last)
98
99 if __name__ == '__main__':
100 tk = Tk()
101 Button(text='Заполнить',command=fill).pack()
102 Label(tk, text='MultiListbox').pack()
103 mlb = MultiListbox(tk, (('Subject', 40), ('Sender', 20), ('Date', 10)))
104
105 tk.mainloop()