2007年11月26日 星期一 10:23
在下面的代码里, 怎么做到选行(现在是只能选列)? 还有,怎么做到选单个格子? 谢谢! Linda from Tkinter import * 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('', lambda e, s=self: s._select(e.y)) lb.bind(' ', lambda e, s=self: s._select(e.y)) lb.bind(' ', lambda e: 'break') lb.bind(' ', lambda e, s=self: s._b2motion(e.x, e.y)) lb.bind(' ', lambda e, s=self: s._button2(e.x, e.y)) frame = Frame(self); frame.pack(side=LEFT, fill=Y) 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) if __name__ == '__main__': tk = Tk() Label(tk, text='MultiListbox').pack() mlb = MultiListbox(tk, (('Subject', 40), ('Sender', 20), ('Date', 10))) for i in range(1000): mlb.insert(END, ('Important Message: %d' % i, 'John Doe', '10/10/%04d' % (1900+i))) mlb.pack(expand=YES,fill=BOTH) tk.mainloop()
2007年11月26日 星期一 12:00
On Nov 26, 2007 10:23 AM, linda. s <samrobertsmith at gmail.com> wrote: > 在下面的代码里, > 怎么做到选行(现在是只能选列)? > 还有,怎么做到选单个格子? > 谢谢! > Linda > 注释掉 __init__ 中的 #lb.bind('', lambda e, s=self: s._select(e.y)) 换成 lb.bind(' ', lambda e, s=self: s.select_col(e)) 添加: def select_col(self, e): print e.widget == self.lists[0] print e.widget == self.lists[1] print e.widget == self.lists[2] self.selection_clear(0, END) e.widget.select_set(0, END) return 'break' e的成员 widget 就是一个 Listbox , 调用 e.select_set(0, END) 可以选择一列, 一列 是一个 Listbox , self.lists 里面有 3 个 Listbox --------------------------------------------------------------------------------------------- 如果 选择 一个 单元格, 只要 像 def selection_set(self, first, last=None): 函数中的 l.selection_set(first, last) 不要 做 for 循环, 只是 选择一个 Listbox 就行
2007年11月26日 星期一 12:03
On Nov 26, 2007 12:00 PM, lubiao <lubiao.py at gmail.com> wrote: > On Nov 26, 2007 10:23 AM, linda. s <samrobertsmith at gmail.com> wrote: > > 在下面的代码里, > > 怎么做到选行(现在是只能选列)? 我 的 $ python Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin Type "help", "copyright", "credits" or "license" for more information. 是能选行, 不能选列 > > 还有,怎么做到选单个格子? > > 谢谢! > > Linda > >
2007年11月26日 星期一 12:09
怎么做到选单个格子? 谢谢! Linda On Nov 25, 2007 8:00 PM, lubiao <lubiao.py在gmail.com> wrote: > On Nov 26, 2007 10:23 AM, linda. s <samrobertsmith在gmail.com> wrote: > > 在下面的代码里, > > 怎么做到选行(现在是只能选列)? > > 还有,怎么做到选单个格子? > > 谢谢! > > Linda > > > 注释掉 __init__ 中的 > #lb.bind('', lambda e, s=self: s._select(e.y)) > 换成 > lb.bind('', lambda e, s=self: s.select_col(e)) > > 添加: > def select_col(self, e): > print e.widget == self.lists[0] > print e.widget == self.lists[1] > print e.widget == self.lists[2] > > self.selection_clear(0, END) > e.widget.select_set(0, END) > > return 'break' > > e的成员 widget 就是一个 Listbox , 调用 e.select_set(0, END) 可以选择一列, > 一列 是一个 Listbox , self.lists 里面有 3 个 Listbox > --------------------------------------------------------------------------------------------- > 如果 选择 一个 单元格, 只要 > 像 def selection_set(self, first, last=None): 函数中的 > l.selection_set(first, last) > 不要 做 for 循环, 只是 选择一个 Listbox 就行 > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
2007年11月26日 星期一 12:43
On Nov 26, 2007 12:09 PM, linda. s <samrobertsmith at gmail.com> wrote: > 怎么做到选单个格子? > 谢谢! > Linda > __init__ 中注释掉: #lb.bind('', lambda e, s=self: s._select(e.y)) 不响应 鼠标 移动的 消息了 修改 lb.bind(' ', lambda e, s=self: s._select(e.y)) 为: lb.bind(' ', lambda e, s=self: s._select(e)) 为了得到 e.widget 修改 def _select(self,e): row = self.lists[0].nearest(e.y) self.selection_clear(0, END) e.widget.select_set(row, None) # 只选择此列此行 #self.selection_set(row) 不做 循环 选择 3 列。 return 'break'
Zeuux © 2025
京ICP备05028076号