Sample code usable Tkinter listbox

rantingrick rantingrick at gmail.com
Mon Mar 1 07:41:26 CET 2010


On Feb 28, 9:57 pm, "Alf P. Steinbach" <al... at start.no> wrote:
> * rantingrick:

I just hate overriding all the paths to each listbox method. How about
just overideing the Listboxe's  geomerty methods once an your done?


#-- start script --#
import Tkinter as tk
from Tkconstants import *
import tkMessageBox as MB

class ScrolledList(tk.Listbox):
    def __init__(self, master, **kw):
        self.frame = tk.Frame(master)
        self.frame.rowconfigure(0, weight=1)
        self.frame.columnconfigure(0, weight=1)
        self.hbar = tk.Scrollbar(self.frame, orient=HORIZONTAL)
        self.block = tk.Frame(self.frame, width=18, height=18)
        self.block.grid(row=1, column=1)
        self.vbar = tk.Scrollbar(self.frame, orient=VERTICAL)
        kw.setdefault('activestyle', 'none')
        kw.setdefault('highlightthickness', 0)
        if 'pack' in kw.keys() and kw.pop('pack') == 1:
            self.frame.pack(fill=BOTH, expand=1)
        tk.Listbox.__init__(self, self.frame, **kw)
        self.grid(row=0, column=0, sticky=N+S+E+W)
        self.hbar.configure(command=self.xview)
        self.vbar.configure(command=self.yview)
        self.config(
            yscrollcommand=self.vbar.set,
            xscrollcommand=self.hbar.set
            )
        self.hbar.grid(row=1, column=0, sticky=W+E)
        self.vbar.grid(row=0, column=1, sticky=N+S)

        self.pack = lambda **kw: self.frame.pack(**kw)
        self.grid = lambda **kw: self.frame.grid(**kw)
        self.place = lambda **kw: self.frame.place(**kw)
        self.pack_config = lambda **kw: self.frame.pack_config(**kw)
        self.grid_config = lambda **kw: self.frame.grid_config(**kw)
        self.place_config = lambda **kw: self.frame.place_config(**kw)
        self.pack_configure = lambda **kw:
self.frame.pack_config(**kw)
        self.grid_configure = lambda **kw:
self.frame.grid_config(**kw)
        self.place_configure = lambda **kw:
self.frame.place_config(**kw)

    def gets(self):
        return self.get(0, END)

    def sets(self, arg):
        self.delete(0, END)
        try:
            arg = arg.strip('\n').splitlines()
        except AttributeError:
            pass
        if hasattr(arg, '__getitem__'):
            for item in arg:
                self.insert(END, str(item))
        else:
            raise TypeError("Scrolledlist.sets() requires a string of
iterable of strings")


if __name__ == '__main__':
    root = tk.Tk()
    listbox = ScrolledList(root, width=50, height=5, fg='green',
selectmode='Extended', pack=1)
    #listbox.sets(1.25)
    #listbox.sets('1\n2\n3\n4\n5\n\n\n')
    listbox.sets(range(100))
    root.mainloop()
#-- end script --#



More information about the Python-list mailing list