July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Brent Burley
You need a
Tkinter widget that works just like a normal
Listbox but with multiple values per row.
When you find a functional limitation in
Tkinter, most often the
best solution is to build your own widget as a Python class,
subclassing an appropriate existing Tkinter widget (often
Frame, so you can easily aggregate several native
Tkinter widgets into your own compound widget) and extending and
tweaking its functionality when necessary. Rather than solving the
problems of just one application, this gives you a reusable component
that you can reuse in many applications. For example,
here’s a way to make a multicolumn equivalent of a
Tkinter Listbox:
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('<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) ...Read now
Unlock full access