问题描述
wxpython 2.8.11.0,python 2.7
wxpython 2.8.11.0, python 2.7
如果我将一些带有一些控件的 Sizer
直接放入 Frame
中,例如
If i put some Sizer
with some controls directly into a Frame
like
import wx
app=wx.App()
frm = wx.Frame(None, title='title')
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(wx.SpinCtrl(frm))
sizer.Add(wx.SpinCtrl(frm))
frm.SetSizerAndFit(sizer)
frm.Show()
app.MainLoop()
Frame
将自动具有正确的最小尺寸以包含 Sizer
并且不可能使其更小.如果中间有一个 Panel
(根据需要在控件之间切换)这不起作用,窗口可能会变得太小.
the Frame
will automagically have a correct minimum size to contain the Sizer
and it is not possible to make it smaller.If there is a Panel
in between (as needed for tabbing between controls) this does not work, the window can be made too small.
import wx
app=wx.App()
frm = wx.Frame(None, title='title')
pan = wx.Panel(frm)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(wx.SpinCtrl(pan))
sizer.Add(wx.SpinCtrl(pan))
pan.SetSizerAndFit(sizer)
frm.Show()
app.MainLoop()
另外 frm.Fit()
和 frm.SetMinSize(frm.GetEffectiveMinSize())
需要获得相同的行为.完整代码:
Additionally frm.Fit()
and frm.SetMinSize(frm.GetEffectiveMinSize())
are required to get the same behavior. Full Code:
import wx
app=wx.App()
frm = wx.Frame(None, title='title')
pan = wx.Panel(frm)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(wx.SpinCtrl(pan))
sizer.Add(wx.SpinCtrl(pan))
pan.SetSizerAndFit(sizer)
frm.Fit()
frm.SetMinSize(frm.GetEffectiveMinSize())
frm.Show()
app.MainLoop()
我对 frm.Fit()
没问题,但我不喜欢 frm.SetMinSize(frm.GetEffectiveMinSize())
.难道没有更好的解决方案让 Frame
自动考虑 Panel
的最小尺寸,就像之前的 Sizer
一样?我正在考虑如果在将另一个控件添加到 sizer 之后 EffectiveMinSize
发生变化会发生什么.
I am okay with frm.Fit()
, but i dislike frm.SetMinSize(frm.GetEffectiveMinSize())
. Isn't there a better solution so that the Frame
automatically considers the minimum size of the Panel
just like with the Sizer
before? I'm considered with what happens if the EffectiveMinSize
changes after another control is added to the sizer.
显然
panel.SetSizerAndFit(sizer)
frm.Fit()
frm.SetMinSize(frm.GetEffectiveMinSize())
应该换成
panel.SetSizer(sizer)
sizer.SetSizeHints(frm)
看起来更干净一些.所以总的来说这看起来像
which looks somewhat cleaner. So in total this looks like
import wx
app=wx.App()
frm = wx.Frame(None, title='title')
pan = wx.Panel(frm)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(wx.SpinCtrl(pan))
sizer.Add(wx.SpinCtrl(pan))
pan.SetSizer(sizer)
sizer.SetSizeHints(frm)
frm.Show()
app.MainLoop()
这是执行此操作的首选方法吗?
Is this the preferred way to do this?
如果稍后添加小部件,即使是直接在 Frame
上使用 Sizer
的第一种方法也无法在没有干预的情况下正确处理,所以我认为这完全是不同的问题.
If widgets are added later on even the first approach with the Sizer
directly on the Frame
doesn't get it right without intervention, so i think that is a totally different question.
推荐答案
最优雅的方式(恕我直言)在 phineas 的回答中给出.使用额外的不需要的 sizer 有点低效,但我认为它不会真的很明显.
The most elegant way (IMHO) is given in phineas' answer. It is slightly inefficient to have an extra otherwise unneeded sizer but I don't think it can really be noticeable.
有些人确实手动调用了 SetSizeHints()
,就像在您的上一个示例中一样,这也有效并且可能更清晰(每当我使用额外的 sizer 时,我觉得有必要发表评论解释为什么不应该删除它).
Some people do call SetSizeHints()
manually, as in your last example, and this works as well and might be more clear (whenever I use an extra sizer I feel the need to leave a comment explaining why it shouldn't be removed).
不幸的是,没有更好的方法,我不确定我们是否可以添加一个,因为您需要对所有面板、sizer 和框架做一些事情,这意味着您不能用一个方法调用而不向其传递无关参数.IE.我们可以有类似的东西
Unfortunately there is no better way and I am not sure if we can even add one because you need to do something with all of the panel, the sizer and the frame and this means that you can't do it with a single method call without passing unrelated parameters to it. I.e. we could have something like
panel.SetSizerAndFitParent(sizer, frame);
但目前还不清楚这是否会更好.
but it's not really clear whether this would be better.
这篇关于wxPython 带面板的框架的最小尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!