Search code examples
pythonuser-interfacewxpythonscrollbar

How to make scrollbars appear in a wx.lib.scrolledpanel.ScrolledPanel which has no widgets but only a straight line?


I create a wx.lib.scrolledpanel.ScrolledPanel. I insert it in its parent panel inside a vertical box sizer. Parent panel's size is width=500, height=500.

Then, in ScrolledPanel, I draw a vertical line with a height of 1000. When I run the app, scrollbars are not displayed on ScrolledPanel. I just see my vertical line with a height of 500, because height of parent panel is 500. There are no widgets in scrolledpanel or parent panel.

Why doesn't the vertical scrollbar appear, so that I can scroll down and see the rest of that vertical line? How do I make it appear?

I even try to set the height of ScrolledPanel to 1000 after I draw the vertical line, but it doesn't work either, something like:

this doesn't work:

create_parent_panel_with_height_500()
create_scrolled_panel()
put_scrolled_panel_on_parent_panel_and_set_sizer()
push_a_button_to_draw_the_vertical_line_with_height_1000()
update_and_refresh_scrolled_panel()

this doesn't work either:

create_parent_panel_with_height_500()
create_scrolled_panel()
put_scrolled_panel_on_parent_panel_and_set_sizer()
push_a_button_to_draw_the_vertical_line_with_height_1000()
set_scrolled_panels_height_to_1000()
update_and_refresh_scrolled_panel()

In both methods, scrollbars don't appear after the vertical line is drawn.


Solution

  • Since you don't have any widgets on the panel then a ScrolledPanel is not the best choice since it bases the virtual and physical sizes of the window on the space needed by the sizer. Using a wx.ScrolledWindow would be a better choice, as you can then control the virtual size yourself.

    If you are stuck with the ScrolledPanel for some reason, it is still doable. You should just need to create a sizer, and add a spacer to it that reserves enough space for your drawing, and assign the sizer to the panel. There is some code in the ScrolledPanel that expects to have at least one child widget that accepts the focus, but that could probably be dealt with in a derived class if there are any issues from that.