我在python + wxpython中创建了一个非常小的应用程序。我需要在dinamically创建一些小部件,这正是我所做的。但是我对scrool有一些问题:当我创建的小部件数量小于滚动窗口的长度时,右边的小部件边框(静态框)被切断,如果我使用鼠标光标手动调整窗口大小,一切正常。当小部件的数量足以覆盖所有页面时,滚动自动出现并且布局是完美的。那么当窗口小部件数量不足时,如何解决问题呢?
import wx
import wx.xrc
import wx.aui
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,
size = wx.Size( 760,760 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
self.number_of_added_mas = 0
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
#Setup Main Container
self.MainSizer = wx.BoxSizer( wx.VERTICAL )
self.ScrolledWindow = wx.ScrolledWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
self.ScrolledWindow.SetScrollRate( 5, 5 )
self.ScrolledWindow.EnableScrolling(True,True)
self.MainVerticalSizer = wx.BoxSizer( wx.VERTICAL )
bookStyle = wx.aui.AUI_NB_DEFAULT_STYLE
bookStyle &= ~(wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)
self.m_auinotebook1 = wx.aui.AuiNotebook( self.ScrolledWindow, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, style=bookStyle )
#Costruzione Master Sequence Tab
self.MasterSequenceTab = wx.ScrolledWindow( self.m_auinotebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
self.MasterSequenceTab.SetScrollRate( 5, 5 )
self.MasterVSizer = wx.BoxSizer( wx.VERTICAL )
MasterButtonSizer = wx.BoxSizer( wx.HORIZONTAL )
self.ButtonAdd = wx.Button( self.MasterSequenceTab, wx.ID_ANY, u"Add", wx.DefaultPosition, wx.DefaultSize, 0 )
MasterButtonSizer.Add( self.ButtonAdd, 0, wx.ALL, 5 )
self.ButtonAdd.Bind(wx.EVT_BUTTON, self.onAddMaster)
self.ButtonRemove = wx.Button( self.MasterSequenceTab, 1, u"Remove", wx.DefaultPosition, wx.DefaultSize, 0 )
MasterButtonSizer.Add( self.ButtonRemove, 0, wx.ALL, 5 )
self.ButtonRemove.Bind(wx.EVT_BUTTON, self.onRemovePanel)
self.MasterVSizer.Add( MasterButtonSizer, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 5 )
self.onAddMaster(self)
self.m_auinotebook1.AddPage( self.MasterSequenceTab, u"Master Sequence", False )
#Finalizzazione finestre
self.MainVerticalSizer.Add( self.m_auinotebook1, 1, wx.EXPAND, 5 )
self.MainSizer.Add( self.ScrolledWindow, 1, wx.EXPAND |wx.ALL, 5 )
def finalizeWindows(self):
self.ScrolledWindow.SetSizer( self.MainVerticalSizer )
self.ScrolledWindow.Layout()
self.MainVerticalSizer.Fit( self.ScrolledWindow )
self.SetSizer( self.MainSizer )
self.Layout()
self.Centre( wx.BOTH )
def onAddMaster(self, event):
self.number_of_added_mas += 1
self.panel_master = wx.Panel( self.MasterSequenceTab, 1+self.number_of_added_mas, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.MastersbSizerAdded = wx.StaticBoxSizer( wx.StaticBox( self.panel_master, 100+self.number_of_added_mas, wx.EmptyString ),wx.VERTICAL )
self.MasterCheckEnableAdded = wx.CheckBox( self.MastersbSizerAdded.GetStaticBox(), 200+self.number_of_added_mas, u"Enable", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterCheckEnableAdded.SetValue(True)
self.MastersbSizerAdded.Add( self.MasterCheckEnableAdded, 0, wx.ALIGN_RIGHT|wx.ALL, 5 )
MasterfgSizerAdded = wx.FlexGridSizer( 0, 2, 0, 0 )
MasterfgSizerAdded.SetFlexibleDirection( wx.BOTH )
MasterfgSizerAdded.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.MasterTextNomeAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), wx.ID_ANY, u"Nome", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextNomeAdded.Wrap( -1 )
MasterfgSizerAdded.Add( self.MasterTextNomeAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlNomeAdded = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 300+self.number_of_added_mas, wx.EmptyString, wx.DefaultPosition, wx.Size( 566,-1 ), 0 )
MasterfgSizerAdded.Add( self.MasterCtrlNomeAdded, 0, wx.ALL, 5 )
self.MasterTextDescrizioneAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), wx.ID_ANY, u"Descrizione ", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextDescrizioneAdded.Wrap( -1 )
MasterfgSizerAdded.Add( self.MasterTextDescrizioneAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlDescrizione1 = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 400+self.number_of_added_mas, wx.EmptyString, wx.DefaultPosition, wx.Size( 566,-1 ), 0 )
MasterfgSizerAdded.Add( self.MasterCtrlDescrizione1, 0, wx.ALL, 5 )
self.MastersbSizerAdded.Add( MasterfgSizerAdded, 0, wx.ALL, 5 )
self.panel_master.SetSizer( self.MastersbSizerAdded )
self.panel_master.Layout()
self.MastersbSizerAdded.Fit( self.panel_master )
self.MasterVSizer.Add( self.panel_master, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 5 )
self.MasterSequenceTab.SetSizer( self.MasterVSizer )
self.MasterSequenceTab.Layout()
self.MasterVSizer.Fit( self.MasterSequenceTab )
self.finalizeWindows()
def onRemovePanel(self, event):
if (event.GetId() == 1) and (self.number_of_added_mas > 0):
panelToDestroy = self.MasterSequenceTab.FindWindowById(1+self.number_of_added_mas)
panelToDestroy.Destroy()
self.number_of_added_mas -= 1
self.finalizeWindows()
if __name__ == '__main__':
app = wx.App()
Example(None, title='Test')
app.MainLoop()