面板上有两个按钮。我想要一个在左边,一个在右边。必须有一个简单的方法,但我看不到它((400,0)spacer只是一个例子;它应该适用于任何面板)
<div class="content content-top content-bottom">
<ch-device-avatar ng-model="client" ch-size="xxlarge" ch-color="light"
ng-click="go(STATES.DEVICES_EDIT_ICON, {'macAddress': macAddress})">
<ch-avatar ch-icon="ion-edit" ch-size="small" ch-color="positive"></ch-avatar>
</ch-device-avatar>
</div>
<form id="client-form" class="form" name="forms.clientForm" ng-submit="save()" novalidate>
<label class="item item-input item-floating-label" ch-input-container>
<span class="input-label">{{ 'main.label-device-name' | i18n }}</span>
<input type="text" name="name" placeholder="{{ 'main.label-device-name' | i18n }}" autocomplete="off"
ng-model="client['name']" ng-maxlength="64" required>
</label>
<div ng-messages="forms.clientForm.name.$error" role="alert" ch-messages="name" multiple>
<div class="hint hint-error" ng-message="required">{{ 'common.error-required' | i18n }}</div>
<div class="hint hint-error" ng-message="maxlength">{{ 'common.error-max-length' | i18n:'65' }}</div>
</div>
</form>
(抱歉,StackOverflow需要更多文字)
Lorem ipsum dolor sit amet,porttitor donec,eget dolor tempor imperdiet,dignissim a sit wisi sed,congue sitting accumsan aliquam urna fusce。 Placerat vel ligula curabitur in nostra nunc,lorem lorem morbi in aliquet。 Etiam ultricies ante sed at aenean id,ligula nibh aenean fusce sapien,ipsum felis sapien eu,mollis ut tempus odio suspendisse fames。 Massa wisi sed nunc maecenas pretium,massa duis vitae non rhoncus nunc,orci sit maecenas ut,aliquet amet,vitae sit。 Quam velit rutrum justo varius felis ligula,ut habitasse mauris,platea nunc。 Et tincidunt vel,libero vitae aliquet donec suspendisse arcu,faucibus orci。 Praesent mollis diam eu justo,quam diam arcu vel tincidunt。 Aliquam eu fusce ipsum,ante turpis sociosqu,quis posuere pellentesque in pellentesque,nulla sit,phasellus lorem inceptos。 Sociis ornare quam magnis eget cursus,ut pretium,velit vitae et sit vivamus,mi malesuada at lectus amet amet acitae。 Convallis tristique gravida cras aenean,amet risus sem vehicula ac lacinia et,elit ac,magna eget ut。
答案 0 :(得分:1)
我总是发现sizer
元素proportion
在这种情况下是你的朋友。它定义了某个元素相对于其他元素应该占用多少空间
这是您的代码,使用了比例选项。
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Demonstrate wxPython button')
self.panel = MainPanel(self)
# self.Fit()
self.Centre()
self.Show()
class MainPanel(wx.Panel):
def __init__(self, frame):
wx.Panel.__init__(self, frame)
# Button
button_sizer = self._button_sizer(frame)
# Main sizer
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(button_sizer,proportion=1, flag=wx.EXPAND)
self.SetSizer(main_sizer)
self.Fit()
def _button_sizer(self, frame):
cmd_save = wx.Button(self, wx.ID_SAVE)
cmd_cancel = wx.Button(self, wx.ID_CANCEL)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
button_sizer.Add(cmd_save)
button_sizer.Add((-1, -1), proportion=1)
button_sizer.Add(cmd_cancel)
return button_sizer
if __name__ == '__main__':
screen_app = wx.App()
main_frame = MainFrame()
screen_app.MainLoop()
答案 1 :(得分:0)
您不想将按钮添加到框架本身。框架应该只有一个孩子,一个面板。在这种情况下,它唯一的子节点应该是MainPanel
类的实例。您可以将对_button_sizer
的通话更改为以下内容:
button_sizer = self._button_sizer(self)
请注意,这里我们将_button_sizer
面板作为其父级而不是框架传递。
当然,如果你想要,你也可以完全在课外推动创建按钮:
import wx
def save_cancel(parent):
cmd_save = wx.Button(parent, wx.ID_SAVE)
cmd_cancel = wx.Button(parent, wx.ID_CANCEL)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
button_sizer.Add(cmd_save)
button_sizer.Add(cmd_cancel, flag=wx.ALIGN_RIGHT)
return button_sizer
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,
'Demonstrate wxPython button')
self.panel = MainPanel(self)
self.Fit()
self.Centre()
self.Show()
class MainPanel(wx.Panel):
def __init__(self, frame):
wx.Panel.__init__(self, frame)
# Button
button_sizer = save_cancel(self)
# Main sizer
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add((400, 0))
main_sizer.Add(button_sizer)
self.SetSizer(main_sizer)
self.Fit()
if __name__ == '__main__':
screen_app = wx.App()
main_frame = MainFrame()
screen_app.MainLoop()
这会使绑定事件变得更加棘手,但这也是使代码更加模块化的一种有趣方式。
如果您希望按钮分开,则添加StretchSpacer
或类似(-1, -1)
的元组(请参阅Rolf的回答)。以下是使用StretchSpacer
:
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,
'Demonstrate wxPython button')
self.panel = MainPanel(self)
self.Fit()
self.Centre()
self.Show()
class MainPanel(wx.Panel):
def __init__(self, frame):
wx.Panel.__init__(self, frame)
# Button
button_sizer = self.save_cancel()
# Main sizer
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add((400, 0))
main_sizer.Add(button_sizer, proportion=1, flag=wx.EXPAND)
self.SetSizer(main_sizer)
self.Fit()
def save_cancel(self):
cmd_save = wx.Button(self, wx.ID_SAVE)
cmd_cancel = wx.Button(self, wx.ID_CANCEL)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
button_sizer.Add(cmd_save)
button_sizer.AddStretchSpacer()
button_sizer.Add(cmd_cancel, flag=wx.ALIGN_RIGHT)
return button_sizer
if __name__ == '__main__':
screen_app = wx.App()
main_frame = MainFrame()
screen_app.MainLoop()