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

时间:2014-10-25 11:07:07

标签: python segmentation-fault wxpython

我想通过使用StaticBox来改进我的GUI。但是因为我添加了一个我无法调用我的布局函数而不会崩溃python(Segementation fault;没有其他错误消息)。 以下行完全重现此错误。我是否正确使用了StaticBoxes? 我需要什么才能让它正常运行?我经常使用嵌套的Sizer 所以布局看起来不错;)。

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

1 个答案:

答案 0 :(得分:0)

您在self._layout()方法中呼叫__init__,并且在那里对我有效。

然后当您单击该按钮时再次调用self._layout,此时您正在尝试将self.box分配给新的box_sizer,但它已经分配给sizer,因此是段错误。