Paul Patterson
Paul Patterson

Reputation: 6928

wxPython: scrollbars interfering with formatting

The following code is intended to demonstrate a problem I'm having with wxPython. When I substitute a wx.Panel with a wx.ScrolledWindow and then run the program the window that is opened is about as small as it could possibly be. Once the frame has been manually resized the program works okay, but obviously I'd prefer the window to open with a sensible size - as it does it I use a subclass of panel instead of a scrolledwindow. I've tried all the obvious stuff like SetBestSize, SetInitialSize but to no avail.

import wx


class MyApp(wx.App):
    def OnInit(self):
        self.frame = Example(None, title="Top frame")
        self.frame.SetInitialSize()
        self.SetTopWindow(self.frame)
        self.frame.Show()

        return True


class Example(wx.Frame):

    def __init__(self, parent, title, ): 
        super(Example, self).__init__(parent, title=title,size=(300, 350))

        self.panelOne = MyPanel(self)

        self.frameSizer = wx.BoxSizer(wx.VERTICAL)
        self.frameSizer.Add(self.panelOne, 1, wx.EXPAND)

        self.SetSizer(self.frameSizer)
        self.frameSizer.Fit(self)

        self.Centre() 
        self.Show()


class MyPanel(wx.ScrolledWindow):

    def __init__(self, parent):
        super(MyPanel, self).__init__(parent)


        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.SetScrollbars(1,1,400,200)
        self.entryGrid = wx.FlexGridSizer(cols = 8, rows = 10)

        for i in range(80):
            x = wx.StaticText(self, id=-1, label=str(i), size=(-1,-1), pos=(-1,-1), style=0, name="")
            self.entryGrid.Add(x, 1, wx.ALL, 20)
        ### widgets here


        self.mainSizer.Add(self.entryGrid)
        # set optimum layout for mainsizer...
        self.SetSizer(self.mainSizer)
        # ...then fit main sizer to the panel.
        self.mainSizer.Fit(self)


if __name__ == '__main__':

    app = MyApp(False)
    app.MainLoop()

Upvotes: 1

Views: 212

Answers (1)

Infinity77
Infinity77

Reputation: 1449

try the following code and see if it does what you want:

HTH.

Andrea.

import wx


class MyApp(wx.App):

    def OnInit(self):

        self.frame = Example(None, title="Top frame")
        self.frame.SetInitialSize()
        self.SetTopWindow(self.frame)
        self.frame.Show()

        return True


class Example(wx.Frame):

    def __init__(self, parent, title, size=(300, 350)): 

        super(Example, self).__init__(parent, title=title)

        self.panelOne = MyPanel(self, size)

        self.frameSizer = wx.BoxSizer(wx.VERTICAL)
        self.frameSizer.Add(self.panelOne, 1, wx.EXPAND)

        self.SetSizer(self.frameSizer)
        self.frameSizer.Layout()

        self.Centre() 
        self.Show()


class MyPanel(wx.ScrolledWindow):

    def __init__(self, parent, size):

        super(MyPanel, self).__init__(parent)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.SetScrollbars(1, 1, 400, 200)

        self.entryGrid = wx.FlexGridSizer(cols=8, rows=10)

        for i in range(80):
            x = wx.StaticText(self, label=str(i))
            self.entryGrid.Add(x, 1, wx.ALL, 20)

        self.mainSizer.Add(self.entryGrid)
        # set optimum layout for mainsizer...
        self.SetSizer(self.mainSizer)

        self.SetSizeHints(*size)


if __name__ == '__main__':

    app = MyApp(False)
    app.MainLoop()

Upvotes: 2

Related Questions