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