wxPython-添加按钮时格式中断

时间:2018-08-29 18:38:32

标签: python formatting modal-dialog wxpython

因此,我有一个简单的模式窗口,其中包含几行文本,一个输入框和几个按钮(保存和取消。)但是,该窗口显示为大部分为空,按钮可见但被边缘剪断窗户的但是当我注释掉按钮时,其他所有内容都显示正常。我无法弄清楚按钮有什么问题,希望更多的眼睛对我有好处。任何帮助表示赞赏。谢谢!

    panel = wx.Panel(self)
    vbox = wx.BoxSizer(wx.VERTICAL)
    # Add explanation text
    vbox.Add(wx.StaticText(panel, label="By default, the sensors record data each minute."), flag=wx.LEFT|wx.TOP, border=10)
    vbox.Add(wx.StaticText(panel, label="You can change the recording interval here."), flag=wx.LEFT, border=10)
    vbox.Add(wx.StaticText(panel, label="(Should be between 1 and 60 minutes)"), flag=wx.LEFT|wx.BOTTOM, border=10)
    # Make a horizontal line
    line = wx.StaticLine(panel)
    vbox.Add(line, flag=wx.LEFT|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=7)
    # Create input
    self.interval_input = wx.SpinCtrl(panel, value=self.interval, min=1, max=60)
    vbox.Add(self.interval_input, 0, wx.ALL|wx.CENTER, 5)
    # Make a horizontal line
    line = wx.StaticLine(panel)
    vbox.Add(line, flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=7)
    # Add save and cancel buttons
    button_area = wx.BoxSizer(wx.HORIZONTAL)
    self.save_button = wx.Button(self, label='Save')
    self.save_button.Bind(wx.EVT_BUTTON, self.OnSave)
    button_area.Add(self.save_button, flag=wx.RIGHT, border=5)
    self.cancel_button = wx.Button(self, label='Cancel')
    self.cancel_button.Bind(wx.EVT_BUTTON, self.OnCancel)
    button_area.Add(self.cancel_button)
    vbox.Add(button_area, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=10)
    # Adjust window size to fit content
    panel.SetSizer(vbox)
    vbox.Fit(self)

1 个答案:

答案 0 :(得分:1)

这被称为“育儿”问题。这些按钮与其他小部件的父级不同,最终导致它们以奇怪的方式堆叠在一起。将保存和取消按钮的父级更改为panel而不是self,它将正常工作。

这是可运行的版本:

import wx

class MyApp(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Test')

        panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)
        # Add explanation text
        vbox.Add(wx.StaticText(panel, label="By default, the sensors record data each minute."), flag=wx.LEFT|wx.TOP, border=10)
        vbox.Add(wx.StaticText(panel, label="You can change the recording interval here."), flag=wx.LEFT, border=10)
        vbox.Add(wx.StaticText(panel, label="(Should be between 1 and 60 minutes)"), flag=wx.LEFT|wx.BOTTOM, border=10)
        # Make a horizontal line
        line = wx.StaticLine(panel)
        vbox.Add(line, flag=wx.LEFT|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=7)
        # Create input
        self.interval_input = wx.SpinCtrl(panel, value="", min=1, max=60)
        vbox.Add(self.interval_input, 0, wx.ALL|wx.CENTER, 5)
        # Make a horizontal line
        line = wx.StaticLine(panel)
        vbox.Add(line, flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=7)
        # Add save and cancel buttons
        button_area = wx.BoxSizer(wx.HORIZONTAL)
        self.save_button = wx.Button(panel, label='Save')
        self.save_button.Bind(wx.EVT_BUTTON, self.OnSave)
        button_area.Add(self.save_button, flag=wx.RIGHT, border=5)
        self.cancel_button = wx.Button(panel, label='Cancel')
        self.cancel_button.Bind(wx.EVT_BUTTON, self.OnCancel)
        button_area.Add(self.cancel_button)
        vbox.Add(button_area, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=10)
        # Adjust window size to fit content
        panel.SetSizer(vbox)
        #vbox.Fit(self)

        self.Show()

    def OnSave(self, event):
        pass

    def OnCancel(self, event):
        pass    

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyApp()
    app.MainLoop()