我试图理解how sizer and sizes work但很明显,我错过了一些至关重要的东西。这就是我得到的:
显然,内部面板(sizer)不适合窗户。我想要的是这个:
是由手动重新设置的。
我该如何实现?
请注意,真实代码有更多不同的wx.StaticBox
,因此对我所做错误的一般解释会比仅仅修复更受欢迎。
以下是玩具示例代码:
import wx
import wx.lib.agw.floatspin as FS
class GUIControl(wx.Frame):
def __init__(self, parent, id, title, *args, **kwargs):
super(GUIControl, self).__init__(parent, id, title, *args, **kwargs)
self.radio = {}
self.modes = ['Open loop', 'Current control', 'Voltage control']
panel = wx.Panel(self)
panel.SetBackgroundColour('#5968c3')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self._ui(panel), 1, wx.EXPAND | wx.ALL, 3)
panel.SetSizer(sizer)
print(sizer.ComputeFittingWindowSize(self))
self.SetSize(sizer.ComputeFittingWindowSize(self))
self.Centre()
self.Show(True)
def _ui(self, parent):
panel = wx.Panel(parent)
vbox = wx.BoxSizer(wx.VERTICAL)
flags = wx.EXPAND | wx.SHAPED
vbox.Add(self._ui_prefix(panel), flag=wx.EXPAND)
vbox.Add(self._ui_control_mode(panel), flag=flags)
vbox.Add(self._ui_suffix(panel), flag=wx.EXPAND)
panel.SetSizer(vbox)
return panel
def _ui_prefix(self, parent):
panel = wx.Panel(parent)
panel.SetBackgroundColour('steelblue')
hbox = wx.BoxSizer(wx.HORIZONTAL)
_label = wx.StaticText(panel, label='')
_label.SetLabelMarkup("<b><i><big>Controls</big></i></b>")
hbox.Add(_label, flag=wx.ALIGN_CENTER | wx.ALL)
panel.SetSizer(hbox)
return panel
def _ui_control_mode(self, parent):
box = wx.StaticBox(parent, -1, "Control mode")
_label_modes = wx.StaticText(box, label='')
_label_modes.SetLabelMarkup("<i>Modes</i>")
for item in self.modes:
self.radio[item] = wx.RadioButton(box, -1, item)
_label_duty = wx.StaticText(box, label='')
_label_duty.SetLabelMarkup("<i>Duty cycle</i>")
self.sc1 = wx.SpinCtrl(box, wx.ID_ANY, '0', min=0, max=100)
_units_duty = wx.StaticText(box, label='%')
_label_current = wx.StaticText(box, label='')
_label_current.SetLabelMarkup("<i>Current</i>")
self.sc2 = FS.FloatSpin(box,
wx.ID_ANY,
value=0.0,
min_val=0.0,
max_val=20.0, # FIXME
increment=0.1)
self.sc2.SetFormat("%f")
self.sc2.SetDigits(1)
_units_current = wx.StaticText(box, label='A')
fgs = wx.FlexGridSizer(5, 3, 5, 7)
flags = wx.EXPAND | wx.ALL | wx.SHAPED
fgs.Add(_label_modes, 0, flags)
fgs.Add(self.radio['Open loop'], 0, flags)
fgs.AddStretchSpacer()
fgs.AddStretchSpacer()
fgs.Add(self.radio['Current control'], 0, flags)
fgs.AddStretchSpacer()
fgs.AddStretchSpacer()
fgs.Add(self.radio['Voltage control'], 0, flags)
fgs.AddStretchSpacer()
fgs.Add(_label_duty, 0, flags)
fgs.Add(self.sc1, 0, flags)
fgs.Add(_units_duty, 0, flags)
fgs.Add(_label_current, 0, flags)
fgs.Add(self.sc2, 0, flags)
fgs.Add(_units_current, 0, flags)
fgs.SetSizeHints(box)
fgs.Layout()
box.SetSizer(fgs)
return box
def _ui_suffix(self, parent):
panel = wx.Panel(parent)
panel.SetBackgroundColour('steelblue')
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(wx.BoxSizer(wx.VERTICAL),
flag=wx.RESERVE_SPACE_EVEN_IF_HIDDEN)
_quit = wx.Button(panel, -1, label=" Quit ")
_quit.SetBackgroundColour('#5968c3')
_quit.Bind(wx.EVT_BUTTON, self.OnQuit)
hbox.AddStretchSpacer()
hbox.Add(_quit, flag=wx.ALIGN_CENTER | wx.SHAPED | wx.ALL, border=3)
hbox.AddStretchSpacer()
panel.SetSizer(hbox)
return panel
def OnQuit(self, e):
self.Close()
if __name__ == '__main__':
app = wx.App()
GUIControl(None, -1, title='Control UI test')
app.MainLoop()
答案 0 :(得分:2)
在这种情况下最简单的方法是给框架一个sizer,添加主面板,然后调用框架的Fit
方法。例如,您可以将self.SetSize
中的GUIControl.__init__
来电替换为:
frameSizer = wx.BoxSizer()
frameSizer.Add(panel, 1, wx.EXPAND)
self.SetSizer(frameSizer)
self.Fit()