1

以下代码旨在演示我在使用 wxPython 时遇到的问题。当我用 a 替换 awx.Panel然后wx.ScrolledWindow运行程序时,打开的窗口尽可能小。手动调整框架大小后,程序就可以正常工作,但显然我更希望窗口以合理的大小打开 - 因为它这样做我使用子类panel而不是scrolledwindow. 我已经尝试了所有明显的东西,例如SetBestSizeSetInitialSize但无济于事。

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()
4

1 回答 1

2

尝试以下代码,看看它是否符合您的要求:

HTH。

安德里亚。

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()
于 2011-12-01T08:35:06.563 回答