如果调用布局,StaticBox会导致分段错误



我想通过使用StaticBoxes来改进我的GUI。但由于我添加了一个,我无法在不崩溃python的情况下调用布局函数(Segmentation错误;没有其他错误消息)。下面几行正好重现了这个错误。我是否正确使用StaticBoxes?我需要什么才能让它正常运行?我经常使用嵌套的Sizers所以布局看起来不错;)。

import wx
class MainWindow(wx.Frame):
    '''test frame'''
    def __init__(self,*args,**kwargs):
        '''Constructor'''
        super(MainWindow,self).__init__(*args,**kwargs)
        self.panel = wx.Panel(self)
        self.box = wx.StaticBox(self.panel, wx.ID_ANY, u'input value1')
        self.button_calc = wx.Button(self.panel, wx.ID_ANY, label=u'calc_xy')
        self.Bind(wx.EVT_BUTTON, self.calculate, self.button_calc)
        self._layout()
    def _layout(self):
        box_sizer = wx.StaticBoxSizer(self.box, wx.VERTICAL)
        sizer = wx.GridBagSizer()
        inside_the_box = wx.GridBagSizer()
        box_sizer.Add(inside_the_box, 5, wx.ALL, 5)
        sizer.Add(box_sizer, (0, 0), (2, 2), wx.EXPAND)
        sizer.Add(self.button_calc, (2, 0))
        self.panel.SetSizerAndFit(sizer)
    def calculate(self, event):
        print '5'
        self._layout()
if __name__ == '__main__':
    app = wx.App()
    frame = MainWindow(None, -1, 'test window')
    frame.Show()
    app.MainLoop()

您在__init__方法中调用self._layout(),它在那里对我很有效。

然后,当您单击按钮时,您再次调用self._layout,此时您正试图将self.box分配给一个新的box_sizer,但它已经分配给了一个sizer,因此是segfault。

最新更新