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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
答案

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

以上是关于wxPython - 添加按钮时格式化中断的主要内容,如果未能解决你的问题,请参考以下文章

添加到片段时按钮不起作用

如何在按钮单击时添加/删除片段?

与 Jetpack 导航一起使用时,TextField 会中断组合吗?

如何在地图片段 API v2 布局顶部添加按钮

以编程方式将按钮添加到片段

Android:向活动添加片段