Sujet : TkInter Scrolled Listbox class?
De : ml (at) *nospam* fam-goebel.de (Ulrich Goebel)
Groupes : comp.lang.pythonDate : 04. Nov 2024, 17:32:48
Autres entêtes
Message-ID : <mailman.76.1730735763.4695.python-list@python.org>
References : 1
User-Agent : Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu)
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>