I have this example program which has several problems, and I’ve been staring at it far too long.
It takes one optional argument, --scrolled, which controls whether or not the red part is a wx.lib.scrolledpanel.
The first problem is that I want the text in the red area to be scrolled up and down, but wrapped to fit the current width of the window. In the non-scrolled version, the wrapping works, but in the scrolled version the text just extends off to the right.
The second problem is that if I don’t use a panel to wrap the statictext then it sizes itself to the frame, but with a panel is sizes itself to the text if the text is smaller than the frame. I think I have the sizers set up exactly the same between the version with the panel and the version without.
A third problem is that the existence of a panel in the top part seems to cause the ratio (2 for the top, 1 for the bottom) to be lost when the frame is too short, the bottom portion disappears off the bottom of the window.
That might be too many things for a single post, but we will give this a try!
Thanks in advance for any insight you might have.
#!/usr/bin/env python3
##
## Check for python 3
##
import sys
assert sys.version_info.major == 3
if "--scrolled" in sys.argv:
scrolled = True
else:
scrolled = False
try:
import wx
except:
print( 'ERROR: python wx module is required' )
sys.exit( 1 )
import wx.lib.scrolledpanel
class scrolled_panel( wx.lib.scrolledpanel.ScrolledPanel ):
def __init__( self, parent ):
super().__init__( parent )
self.sizer = wx.BoxSizer( wx.VERTICAL )
self.SetSizer( self.sizer )
class unscrolled_panel( wx.Panel ):
def __init__( self, parent ):
super().__init__( parent )
self.sizer = wx.BoxSizer( wx.VERTICAL )
self.SetSizer( self.sizer )
class wx_frame( wx.Frame ):
def _button( self, event ):
print( self, self._button )
def __init__(self, parent, id, title, requested_size=None):
scaledown = 1.0
minsize = (600,480)
maxsize = wx.DisplaySize()
if not requested_size:
size = (int( maxsize[0] * .333), int( maxsize[1] * .666 ))
else:
size = requested_size
xsize = size[0]
ysize = size[1]
if xsize > maxsize[0]:
warning( self.__init, "requested width is larger than display (%d versus %d)" % (xsize, maxsize[0]) )
xsize = maxsize[0]
if ysize > maxsize[1]:
warning( self.__init, "requested height is larger than display (%d versus %d)" % (ysize, maxsize[1]) )
ysize = maxsize[1]
if xsize < minsize[0] or ysize < minsize[0]:
if requested_size:
warning( self.__init__, "requested width or height is too small (%s versus %s)" % (str( size ), str( minsize )) )
xsize = minsize[0]
ysize = minsize[1]
super().__init__( parent, title=title, size=(xsize,ysize) )
self.SetBackgroundColour( wx.BLUE )
self.SetMinSize( minsize )
self.SetMaxSize( maxsize )
self.vbox = wx.BoxSizer( wx.VERTICAL )
self.panel = wx.Panel( self )
if scrolled:
self.top_panel = scrolled_panel( self.panel )
else:
self.top_panel = unscrolled_panel( self.panel )
self.panel_text = wx.StaticText( self.top_panel , label="This is a line with a newline at the end that is repeated many times.\n" * 20 + "This is a short line many times with no newline. " * 10, style=wx.ALIGN_LEFT )
self.panel_text.SetBackgroundColour( wx.RED )
self.panel_text.SetFont( wx.Font( 13, wx.DEFAULT, wx.NORMAL, wx.DEFAULT ) )
self.top_panel.sizer.Add( self.panel_text, 1, wx.ALIGN_TOP, wx.ALL | wx.EXPAND, 10 )
if scrolled:
self.top_panel.SetupScrolling( scroll_x=False )
self.panel_text.Wrap( self.top_panel.Size[0] )
self.vbox.Add( self.top_panel, 2, wx.EXPAND | wx.ALL, 10 )
self.lower_text = wx.StaticText( self.panel, label="" )
self.lower_text.SetBackgroundColour( wx.GREEN )
self.vbox.Add( self.lower_text, 1, wx.EXPAND | wx.ALL, 10 )
self.panel.SetSizer( self.vbox )
self.Layout()
self.Centre()
if __name__ == '__main__':
app = wx.App()
frame = wx_frame( None, -1, "Testing Example" )
frame.Show()
app.MainLoop()