【问题标题】:wxPython - Align buttons in an expanding sizerwxPython - 在扩展大小调整器中对齐按钮
【发布时间】:2016-01-21 22:12:43
【问题描述】:

我正在尝试制作一个类似于 wx.MessageDialog 的对话框。底部有一个部分颜色不同,但在我的代码中,按钮不会配合。

按钮应向右对齐,底部应展开为灰色。可能有一个简单的解决方案,任何人都可以看到我的错误吗?

这是设置为展开的sizer

这是设置为不展开的sizer

这是演示问题的代码的工作简化版本。麻烦的代码在底部,用“===”包围:

import wx

class TestDialog(wx.Dialog):
    def __init__(self, parent, msg, title):
        wx.Dialog.__init__(self, parent, id=-1, title=title)
        # outer sizer, this is to allow a spot at the bottom for the buttons
        outerSizer = wx.BoxSizer(wx.VERTICAL)

        # Main sizer for the message dialog
        mainSizer = wx.BoxSizer(wx.HORIZONTAL)

        staticIcon = wx.StaticBitmap(self, bitmap=wx.ArtProvider.GetBitmap(wx.ART_INFORMATION), size=(32,32))
        mainSizer.Add(staticIcon, flag=wx.ALL, border=10)

        # Sizer to hold the message and buttons
        controlsSizer = wx.BoxSizer(wx.VERTICAL)

        # Static text field to show the (error/warning) message on the message dialog
        errorText = wx.StaticText(self, -1, msg, wx.DefaultPosition, wx.DefaultSize, 0)
        errorText.Wrap(600)

        # This is a sizer so we can nest it inside the controls sizer. This allows us to use multiple border flags
        errorTextSizer = wx.BoxSizer(wx.HORIZONTAL)
        errorTextSizer.Add(errorText, flag=wx.TOP, border=15)

        # Add the error text to the controls sizer
        controlsSizer.Add(errorTextSizer, flag=wx.RIGHT, border=10)

        # Outer button panel, to get that slighty greyed look
        outerButtonPanel = wx.Panel(self)
        outerButtonPanelSizer = wx.BoxSizer(wx.HORIZONTAL)
        outerButtonPanel.SetSizer(outerButtonPanelSizer)

        # Button for the "yes" option
        btnYes = wx.Button(outerButtonPanel, label='Yes')
        btnYes.Bind(wx.EVT_BUTTON, self.__yes)
        outerButtonPanelSizer.Add(btnYes, flag=wx.ALIGN_RIGHT | wx.ALL, border=15)

        # Button for the "no" option
        btnNo = wx.Button(outerButtonPanel, label='No')
        btnNo.Bind(wx.EVT_BUTTON, self.__no)
        outerButtonPanelSizer.Add(btnNo, flag=wx.ALIGN_RIGHT | wx.RIGHT | wx.TOP | wx.BOTTOM, border=15)

        outerButtonPanel.SetBackgroundColour(wx.Colour(100, 100, 100)) # find decent colour

        # Add all the sizers to each other, finish up
        mainSizer.Add(controlsSizer)
        outerSizer.Add(mainSizer)

        # ====================================================================
        outerSizer.Add(outerButtonPanel, flag=wx.ALIGN_RIGHT | wx.EXPAND)
        # ====================================================================
        #outerSizer.Add(outerButtonPanel, flag=wx.ALIGN_RIGHT)
        # ====================================================================

        # Done layout
        self.SetSizerAndFit(outerSizer)
        self.CenterOnScreen()

    def __yes(self, evt):
        self.EndModal(wx.ID_YES)

    def __no(self, evt):
        self.EndModal(wx.ID_NO)

if __name__ == '__main__':
    app = wx.App()
    dlg = TestDialog(None, "test test test test test test test test test test test test test test test test", "Test Title")
    val = dlg.ShowModal()
    print "Dialog result: " + str(val == wx.ID_YES)
    app.Exit()

【问题讨论】:

  • 我无法在 Windows 或 Xubuntu 上复制此问题。它只是工作。
  • 嗯,也许这实际上是一个错误,特定于版本。在这里使用 wx 3.0.2。

标签: button alignment wxpython sizer


【解决方案1】:

最后,解决方案。 wx 显然不赞成将多个水平框尺寸器与核心面板嵌套在一起。更具体地说,当您尝试对齐某些内容并将其设置为展开时,它不喜欢它。据我所知,您必须嵌套多个面板和垂直框尺寸器(为按钮使用水平框尺寸器)。

这是上面的示例,但工作正常。从字面上看,改变

outerButtonPanelSizer = wx.BoxSizer(wx.VERTICAL)

outerButtonPanelSizer = wx.BoxSizer(wx.HORIZONTAL)

整个事情都崩溃了。

import wx

class TestDialog(wx.Dialog):
    def __init__(self, parent, msg, title):
        wx.Dialog.__init__(self, parent, id=-1, title=title)
        # outer sizer, this is to allow a spot at the bottom for the buttons
        outerSizer = wx.BoxSizer(wx.VERTICAL)

        # Main sizer for the message dialog
        mainSizer = wx.BoxSizer(wx.HORIZONTAL)

        staticIcon = wx.StaticBitmap(self, bitmap=wx.ArtProvider.GetBitmap(wx.ART_INFORMATION), size=(32,32))
        mainSizer.Add(staticIcon, flag=wx.ALL, border=10)

        # Sizer to hold the message and buttons
        controlsSizer = wx.BoxSizer(wx.VERTICAL)

        # Static text field to show the (error/warning) message on the message dialog
        errorText = wx.StaticText(self, -1, msg, wx.DefaultPosition, wx.DefaultSize, 0)
        errorText.Wrap(600)

        # This is a sizer so we can nest it inside the controls sizer. This allows us to use multiple border flags
        errorTextSizer = wx.BoxSizer(wx.HORIZONTAL)
        errorTextSizer.Add(errorText, flag=wx.TOP, border=15)

        # Add the error text to the controls sizer
        controlsSizer.Add(errorTextSizer, flag=wx.RIGHT, border=10)

        # Outer button panel, to get that slighty greyed look
        outerButtonPanel = wx.Panel(self)
        outerButtonPanelSizer = wx.BoxSizer(wx.VERTICAL)
        outerButtonPanel.SetSizer(outerButtonPanelSizer)

        # inner panel for the buttons (this allows us to right align the buttons
        innerButtonPanel = wx.Panel(outerButtonPanel)
        innerButtonPanelSizer = wx.BoxSizer(wx.HORIZONTAL)

        # Button for the "yes" option
        btnYes = wx.Button(innerButtonPanel, label='Yes')
        btnYes.Bind(wx.EVT_BUTTON, self.__yes)
        innerButtonPanelSizer.Add(btnYes, flag=wx.ALL, border=15)

        # Button for the "no" option
        btnNo = wx.Button(innerButtonPanel, label='No')
        btnNo.Bind(wx.EVT_BUTTON, self.__no)
        innerButtonPanelSizer.Add(btnNo, flag=wx.RIGHT | wx.TOP | wx.BOTTOM, border=15)

        # Add the inner button panel to the outer button panel and align it right
        innerButtonPanel.SetSizer(innerButtonPanelSizer)
        outerButtonPanelSizer.Add(innerButtonPanel, flag=wx.ALIGN_RIGHT)

        outerButtonPanel.SetBackgroundColour(wx.Colour(100, 100, 100)) # find decent colour

        # Add all the sizers to each other, finish up
        mainSizer.Add(controlsSizer)
        outerSizer.Add(mainSizer)

        # ====================================================================
        outerSizer.Add(outerButtonPanel, flag=wx.EXPAND)

        # Done layout
        self.SetSizerAndFit(outerSizer)
        self.CenterOnScreen()

    def __yes(self, evt):
        self.EndModal(wx.ID_YES)

    def __no(self, evt):
        self.EndModal(wx.ID_NO)

if __name__ == '__main__':
    app = wx.App()
    dlg = TestDialog(None, "test test test test test test test test test test test test test test test test", "Test Title")
    val = dlg.ShowModal()
    print "Dialog result: " + str(val == wx.ID_YES)
    app.Exit()

【讨论】:

    【解决方案2】:

    您可以尝试在按钮的左侧放置一个垫片。

            outerButtonPanelSizer.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
    

    【讨论】:

      猜你喜欢
      • 2012-01-28
      • 2012-04-23
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      相关资源
      最近更新 更多