• TkInter Scrolled Listbox class?

    From Ulrich Goebel@ml@fam-goebel.de to comp.lang.python on Mon Nov 4 16:32:48 2024
    From Newsgroup: comp.lang.python

    Hi,

    I would like to build a class ScrolledListbox, which can be packed somewhere in ttk.Frames. What I did is to build not really a scrolled Listbox but a Frame containing a Listbox and a Scrollbar:

    class FrameScrolledListbox(ttk.Frame):
    def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    #
    # build Listbox and Scrollbar
    self.Listbox = tk.Listbox(self)
    self.Scrollbar = ttk.Scrollbar(self)
    #
    # configure these two
    self.Listbox.config(yscrollcommand=self.Scrollbar.set)
    self.Scrollbar.config(command=self.Listbox.yview)
    #
    # pack them in Frame
    self.Listbox.pack(side=tk.LEFT, fill=tk.BOTH)
    self.Scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH)

    That works, so instances of FrameScrolledListbox can be packed and the tk.Listbox itself is accessible via an attribute:

    frmScrolledListbox = FrameScrolledListbox(main) frmScrolledListbox.Listbox.config(...)

    But it would be a bit nicer to get a class like

    class ScrolledListbox(tk.Listbox):
    ...

    So it would be used that way:

    scrolledListbox = ScrolledListbox(main)
    scrolledListbox.config(...)

    Is that possible? The problem which I can't handle is to handle the Frame which seems to be needed to place the Scrollbar somewhere.

    Best regards
    Ulrich
    --
    Ulrich Goebel <ml@fam-goebel.de>
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Cameron Simpson@cs@cskk.id.au to comp.lang.python on Tue Nov 5 08:21:28 2024
    From Newsgroup: comp.lang.python

    On 04Nov2024 16:32, Ulrich Goebel <ml@fam-goebel.de> wrote:
    I would like to build a class ScrolledListbox, which can be packed
    somewhere in ttk.Frames. What I did is to build not really a scrolled >Listbox but a Frame containing a Listbox and a Scrollbar:

    That's what I would build too.

    class FrameScrolledListbox(ttk.Frame):
    def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    # build Listbox and Scrollbar
    self.Listbox = tk.Listbox(self)
    self.Scrollbar = ttk.Scrollbar(self)
    [...]
    But it would be a bit nicer to get a class like

    class ScrolledListbox(tk.Listbox):
    ...

    So it would be used that way:

    scrolledListbox = ScrolledListbox(main)
    scrolledListbox.config(...)

    Probably you want to proxy various methods to the enclosed widgets.
    Possibly you want to do that with parameters in `__init__` also.

    Example:

    class FrameScrolledListbox(ttk.Frame):
    def __init__(self, *frame_args, *, height=None, jump=None, **frame_kw):
    super().__init__(*frame_args, **frame_kw)
    self.Listbox = tk.Listbox(self, height=height)
    self.Scrollbar = ttk.Scrollbar(self, jump=jump)
    ........

    def config(self, *a, **kw):
    return self.Listbox.config(*a, **kw)

    and so forth for the various listbox methods you want to proxy to the
    listbox itself. You could pass scroll specific methods to the scrollbar
    as well.

    Cheers,
    Cameron Simpson <cs@cskk.id.au>
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Alan Gauld@learn2program@gmail.com to comp.lang.python on Tue Nov 5 00:06:56 2024
    From Newsgroup: comp.lang.python

    On 04/11/2024 15:32, Ulrich Goebel via Python-list wrote:

    I would like to build a class ScrolledListbox,

    I assume like the one that used to be available via the Tix module?
    It's a great shame that Tix is gone, it had a lot of these useful
    widgets, but they were all wrappers around Tcl/Tk's Tix module and
    that is the thing that has gone.

    If you really want to replace it I'd suggest looking at the IDLE
    code and see what they did because the IDLE settings dialogues
    have scrolled lists and the IDLE guys have probably done a decent
    job in building their widgets. (Based on the fact that IDLE has
    had a tone of work done in recent years using ttk)
    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Vishal Chandratreya@vpaijc@gmail.com to comp.lang.python on Tue Nov 5 23:58:38 2024
    From Newsgroup: comp.lang.python

    This is what I created ScrollableContainers for. Its usage deviates a bit
    from standard Tkinter practices in that you add widgets to the frame
    attribute of a ScrollableFrameTk instance. 

    [1]twitter.abaf4b19.webp
    [2]ScrollableContainers
    pypi.org

    For your use case, you could populate your list box and then put it inside
    that frame. I’ve not tested this scenario, so I’d appreciate feedback!
    Thanks. 

    On 4 Nov 2024, at 21:28, Ulrich Goebel via Python-list
    <python-list@python.org> wrote:

    Hi,
    I would like to build a class ScrolledListbox, which can be packed
    somewhere in ttk.Frames. What I did is to build not really a scrolled
    Listbox but a Frame containing a Listbox and a Scrollbar:
    class FrameScrolledListbox(ttk.Frame):
       def __init__(self, *args, **kwargs):
           super().__init__(*args, **kwargs)
           #
           # build Listbox and Scrollbar
           self.Listbox = tk.Listbox(self)
           self.Scrollbar = ttk.Scrollbar(self)
           #
           # configure these two
           self.Listbox.config(yscrollcommand=self.Scrollbar.set)
           self.Scrollbar.config(command=self.Listbox.yview)
           #
           # pack them in Frame
           self.Listbox.pack(side=tk.LEFT, fill=tk.BOTH)
           self.Scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH)
    That works, so instances of FrameScrolledListbox can be packed and the
    tk.Listbox itself is accessible via an attribute:
    frmScrolledListbox = FrameScrolledListbox(main)
    frmScrolledListbox.Listbox.config(...)
    But it would be a bit nicer to get a class like
    class ScrolledListbox(tk.Listbox):
       ...
    So it would be used that way:
    scrolledListbox = ScrolledListbox(main)
    scrolledListbox.config(...)
    Is that possible? The problem which I can't handle is to handle the
    Frame which seems to be needed to place the Scrollbar somewhere.
    Best regards
    Ulrich
    --
    Ulrich Goebel <ml@fam-goebel.de>
    --
    https://mail.python.org/mailman/listinfo/python-list

    References

    Visible links
    1. https://pypi.org/project/ScrollableContainers/
    2. https://pypi.org/project/ScrollableContainers/
    --- Synchronet 3.20a-Linux NewsLink 1.114