Button wxPython-在扩展尺寸器中对齐按钮
我正在尝试创建一个类似于wx.MessageDialog的对话框。底部有一个部分颜色不同,但在我的代码中,按钮无法配合 按钮应向右对齐,底部部分应扩展为灰色。可能有一个简单的解决方案,有人能看到我的错误吗 这是在尺寸设定为展开的情况下进行的: 这是在尺寸设定为不展开的情况下进行的: 下面是代码的简化版本,它演示了这个问题。麻烦代码在底部,用“==”包围:Button wxPython-在扩展尺寸器中对齐按钮,button,alignment,wxpython,sizer,Button,Alignment,Wxpython,Sizer,我正在尝试创建一个类似于wx.MessageDialog的对话框。底部有一个部分颜色不同,但在我的代码中,按钮无法配合 按钮应向右对齐,底部部分应扩展为灰色。可能有一个简单的解决方案,有人能看到我的错误吗 这是在尺寸设定为展开的情况下进行的: 这是在尺寸设定为不展开的情况下进行的: 下面是代码的简化版本,它演示了这个问题。麻烦代码在底部,用“==”包围: 最后,给出了解决方案。wx显然不赞成将多个水平盒大小器与核心面板嵌套在一起。更具体地说,当您尝试对齐某个对象并将其设置为展开时,它不喜欢它
最后,给出了解决方案。wx显然不赞成将多个水平盒大小器与核心面板嵌套在一起。更具体地说,当您尝试对齐某个对象并将其设置为展开时,它不喜欢它。据我所知,您必须嵌套多个面板和垂直框大小调整器(带有用于按钮的水平框大小调整器) 这是上面的例子,但工作正常。从字面上说,改变
outerButtonPanelSizer = wx.BoxSizer(wx.VERTICAL)
到
整个事情都破裂了
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 )
我无法在Windows或Xubuntu上复制此问题。它只是工作。嗯,也许这实际上是一个bug,特定于版本。这里使用WX3.0.2。
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 )