Tkinter, problem when using 2 scrollbars

Jeff Epler jepler at unpythonic.net
Mon Oct 21 17:51:25 CEST 2002


You could do something like this:

    import Tkinter

    __all__ = ['Scrolled']

    def Scrolled(constructor, parent=None, *args, **kw):
        f = Tkinter.Frame(parent)
        w = constructor(f, *args, **kw)
        vs = Tkinter.Scrollbar(f, orient="v", command=w.yview)
        hs = Tkinter.Scrollbar(f, orient="h", command=w.xview)
        w.configure(yscrollcommand=vs.set, xscrollcommand=hs.set)

        f.grid_rowconfigure(0, weight=1)
        f.grid_columnconfigure(0, weight=1)

        w.grid(sticky="nsew", row=0, column=0)
        vs.grid(sticky="ns", row=0, column=1)
        hs.grid(sticky="ew", row=1, column=0)

        return w, f

    def test():
        t = Tkinter.Tk()
        w, f = Scrolled(Tkinter.Canvas, t, scrollregion=(0,0,800,800), bg="red")
        w.create_rectangle((100, 100, 700, 700), fill="white")
        f.pack(expand=1, fill="both")
        t.mainloop()

    if __name__ == '__main__': test()

> I guess I really want to know how to invoke constructors to unknown
> class types, where the class type is derived through a string.

Just pass the constructor, as in
    Scrolled(Tkinter.Canvas, ...)
You can use getattr if you really have to take a string as your input,
though:

    >>> Canvas = getattr(Tkinter, "Canvas")
    >>> print Canvas is Tkinter.Canvas
    True

Jeff




More information about the Python-list mailing list