Package teamwork :: Package widgets :: Module cookbook
[hide private]
[frames] | no frames]

Source Code for Module teamwork.widgets.cookbook

  1  from Tkinter import * 
  2  import Pmw 
  3   
4 -class MultiListbox(Pmw.MegaWidget):
5 - def __init__(self, master, lists, **kw):
6 optiondefs = ( 7 # Callbacks (dp) 8 ('doublecommand', None, None), 9 ('rowselectcommand', None, None), 10 ('colselectcommand', None, None), 11 ) 12 self.defineoptions(kw, optiondefs) 13 Pmw.MegaWidget.__init__(self, master) 14 self.lists = [] 15 for label,width in lists: 16 self.addList(label,width) 17 frame = Frame(self.component('hull')) 18 frame.pack(side='right', fill=Y) 19 Label(frame, borderwidth=1, relief='raised').pack(fill=X) 20 sb = Scrollbar(frame, orient='vertical', command=self._scroll) 21 sb.pack(expand='yes', fill='y') 22 self.lists[0]['yscrollcommand']=sb.set
23
24 - def addList(self,label,width):
25 """dp""" 26 try: 27 frame = self.createcomponent('frame %s' % (label),(),None,Frame, 28 (self.component('hull'),)) 29 except ValueError: 30 # Already exists 31 return 32 frame.pack(side='left', expand='yes', fill='both') 33 widget = self.createcomponent('label %s' % (label),(),label,Label, 34 (frame, ), 35 text=label, borderwidth=1, 36 anchor='w',height=1,width=width, 37 relief='raised',wraplength=0) 38 widget.bind('<Button-1>', lambda e,s=self,l=label:s.selectColumn(e,l)) 39 widget.pack(fill='x') 40 lb = self.createcomponent('list %s' % (label),(),label,Listbox, 41 (frame, ), 42 width=width, borderwidth=0, 43 selectborderwidth=0, 44 relief='flat', exportselection='false') 45 lb.pack(expand='yes', fill='both') 46 self.lists.append(lb) 47 lb.bind('<Double-ButtonRelease-1>', lambda e, s=self: s._double(e)) 48 lb.bind('<B1-Motion>', lambda e, s=self: s._select(e.y)) 49 lb.bind('<Button-1>', lambda e, s=self: s._select(e.y)) 50 lb.bind('<Shift-1>', lambda e, s=self: s._select(e.y,'shift')) 51 lb.bind('<Control-1>', lambda e, s=self: s._select(e.y,'ctrl')) 52 lb.bind('<Leave>', lambda e: 'break') 53 lb.bind('<B2-Motion>', lambda e, s=self: s._b2motion(e.x, e.y)) 54 lb.bind('<Button-2>', lambda e, s=self: s._button2(e.x, e.y))
55
56 - def delList(self,label):
57 """dp""" 58 widget = self.component('list %s' % (label)) 59 for index in range(len(self.lists)): 60 if self.lists[index] is widget: 61 break 62 else: 63 raise UserWarning,'Unable to find list %s to delete' % (label) 64 del self.lists[index] 65 self.destroycomponent('list %s' % (label)) 66 self.destroycomponent('label %s' % (label)) 67 self.destroycomponent('frame %s' % (label))
68
69 - def selectColumn(self,event,label):
70 """dp""" 71 self.selection_clear(0,'end') 72 widget = self.component('list %s' % (label)) 73 widget.selection_set(0,'end') 74 if self['colselectcommand']: 75 self['colselectcommand'](label)
76
77 - def _select(self, y,modifier=None):
78 row = self.lists[0].nearest(y) 79 if modifier is None: 80 self.selection_clear(0, 'end') 81 self.selection_set(row) 82 elif modifier == 'shift': 83 selection = list(self.curselection()) 84 if row < selection[0]: 85 self.selection_set(row,selection[0]) 86 else: 87 self.selection_set(selection[0],row) 88 if self['rowselectcommand']: 89 self['rowselectcommand'](row) 90 return 'break'
91
92 - def _double(self,event,modifier=None):
93 """dp""" 94 row = self.lists[0].nearest(event.y) 95 if self['doublecommand']: 96 self['doublecommand'](row,event) 97 return 'break'
98
99 - def _button2(self, x, y):
100 for l in self.lists: l.scan_mark(x, y) 101 return 'break'
102
103 - def _b2motion(self, x, y):
104 for l in self.lists: l.scan_dragto(x, y) 105 return 'break'
106
107 - def _scroll(self, *args):
108 for l in self.lists: 109 apply(l.yview, args)
110
111 - def curselection(self):
112 return self.lists[0].curselection()
113
114 - def delete(self, first, last=None):
115 for l in self.lists: 116 l.delete(first, last)
117
118 - def get(self, first, last=None):
119 result = [] 120 for l in self.lists: 121 result.append(l.get(first,last)) 122 if last: return apply(map, [None] + result) 123 return result
124
125 - def index(self, index):
126 self.lists[0].index(index)
127
128 - def insert(self, index, *elements):
129 for e in elements: 130 i = 0 131 for l in self.lists: 132 l.insert(index, e[i]) 133 i = i + 1
134
135 - def size(self):
136 return self.lists[0].size()
137
138 - def see(self, index):
139 for l in self.lists: 140 l.see(index)
141
142 - def selection_anchor(self, index):
143 for l in self.lists: 144 l.selection_anchor(index)
145
146 - def selection_clear(self, first, last=None):
147 for l in self.lists: 148 l.selection_clear(first, last)
149
150 - def selection_includes(self, index):
151 return self.lists[0].selection_includes(index)
152
153 - def selection_set(self, first, last=None):
154 for l in self.lists: 155 l.selection_set(first, last)
156 157 if __name__ == '__main__': 158 tk = Tk() 159 Label(tk, text='MultiListbox').pack() 160 mlb = MultiListbox(tk, (('Subject', 40), ('Sender', 20), ('Date', 10))) 161 for i in range(1000): 162 mlb.insert(END, ('Important Message: %d' % i, 'John Doe', '10/10/%04d' % (1900+i))) 163 mlb.pack(expand=YES,fill=BOTH) 164 tk.mainloop() 165