wxPython - 在扩展尺寸器中对齐按钮

wxPython - Align buttons in an expanding sizer

我正在尝试制作一个看起来类似于 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()

终于解决了。 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()

你能不能试试在按钮的左边放一个垫片。

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