Sample code usable Tkinter listbox

rantingrick rantingrick at gmail.com
Sat Mar 6 23:46:29 CET 2010


Opps: found a few errors in that last ScrolledListbox class, try this
one...



import Tkinter as tk
from Tkconstants import *

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)
        kw.setdefault('pack', 0)
        if 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 or
iterable of strings")


if __name__ == '__main__':
    root = tk.Tk()
    listbox = ScrolledList(root, width=50, height=10, pack=1)
    #listbox.sets(1.25)
    #listbox.sets('1\n2\n3\n4\n5\n\n\n')
    listbox.sets(range(100))
    #listbox.grid(row=0, column=0)
    root.mainloop()



More information about the Python-list mailing list