Orel Fichman
Orel Fichman

Reputation: 160

PanedWindow has no children when a custom class widget is added

This is a very peculiar issue that I'm facing, even though it might be because of my lack of knowledge on the inner workings of TKinter.

I'm trying to create a class called ScrollableFrame which inherits from Frame and add it to a PanedWindow widget.

Say I add a normal Frame:

main_panel = PanedWindow()
main_panel.pack()
settings_frame = Frame()
main_panel.add(settings_frame)

That code works and when I invoke main_panel.children I can see the frame.

When I use my ScrollableFrame though, like this:

class ScrollableFrame(Frame):
    def __init__(self, **kw):
        try:
            super().__init__(**kw)
            self._canvas = Canvas(self)
            self._frame = Frame(self._canvas)
            self._scrollbar = Scrollbar(self._canvas, orient=VERTICAL, command=self._canvas.yview)
            self._canvas.configure(yscrollcommand=self._scrollbar.set)
        except Exception as e:
            print(e)


main_panel = PanedWindow()
main_panel.pack()
settings_frame = ScrollableFrame()
main_panel.add(settings_frame)

main_panel reports having no children at all when I invoke main_panel.children.

Has anyone ever encountered this? Thanks in advance.

Upvotes: 0

Views: 253

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386362

The panel reports having no children because it has no children. You aren't passing the panedwindow as a parent to ScrollableFrame so that scrolled frame is a child of the root window.

If you want the scrollable frame to be a child of the paned window, you need to make the paned window its parent. You need to pass main_panel to ScrollableFrame. Since your implementation of ScrollableFrame doesn't take positional arguments you have to specify the parent as the master keyword argument:

settings_frame = ScrollableFrame(master=main_panel)

You also have the problem that you aren't calling pack, place, or grid for the canvas, frame, and scrollbar. That doesn't affect the parent/child issue you're asking about, but it will prevent those widgets from being visible.

You also shouldn't put the scrollbar inside the canvas. That will cause the scrollbar to be on top of objects drawn near the right margin.

Upvotes: 1

Related Questions