wxPython - 添加按钮时格式化中断?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (46)

所以我有一个简单的模态窗口,有几行文本,一个输入框和几个按钮(保存和取消。)但是,窗口显示大部分为空,按钮可见但被窗口边缘剪切。但是当我注释掉按钮时,其他一切都显示得很好。我无法弄清楚我的按钮有什么问题,我希望有一些额外的眼睛会对我有所帮助。任何帮助表示赞赏。谢谢!

    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)
提问于
用户回答回答于

这被称为“育儿”问题。这些按钮与其他小部件没有相同的父级,最终导致它们以奇怪的方式堆叠起来。更改保存和取消按钮的父母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()

扫码关注云+社区

领取腾讯云代金券