wxPython:wx.PyControl可以包含wx.Sizer吗? [英] wxPython: Can a wx.PyControl contain a wx.Sizer?
问题描述
wx.PyControl
可以包含 wx.Sizer
吗?
请注意,我最终在这里尝试做的事情(带有浮点数的纺锤)已经在另一个问题中得到了回答。我对在 wx.PyControl
内布置小部件特别感兴趣,如果我遇到需要制作自己的自定义小部件的技能,该技能可能会很有用。我已经读过创建CustomControls ,但是它没有在 wx内使用定尺寸器.PyControl
子类。
使用下面的代码,我的 CustomWidget
不会向右看。我尚未执行 DoGetBestSize
,因为我认为这适用于 wx.Sizer
对小部件。我实际上是在内部 CustomWidget
内执行 wx.Sizer
的工作。 p>
这是我的代码(子控件之间没有事件绑定):
编辑:这是我的更正后的类代码,这要感谢 Steven Sproat :
import wx
class CustomWidget(wx.PyControl):
def __init __(self ,父级):
wx.PyControl .__ init __(self,parent = parent,style = wx.NO_BORDER)#添加了样式。
text = wx.TextCtrl(parent = self)
spin = wx.SpinButton(parent = self,style = wx.SP_VERTICAL)
sizer = wx.GridBagSizer()
self .layout(text,spin,sizer)
self.OnInit(text,sizer)
def OnInit(self,text,sizer):
text.SetValue(u 0.000 )
def布局(self,text,spin,sizer):
self.SetSizer(sizer)
sizer.Add(text,pos =(0,0), flag = wx.ALIGN_CENTER)
sizer.Add(spin,pos =(0,1),flag = wx.ALIGN_CENTER)
self.Fit()
self.Layout()#这是我所缺乏的。我需要致电.Layout()
self.CenterOnParent()
是的,可以。您只需要调用Layout()来告诉sizer重新计算/布局其子级。
import wx
class Frame(wx.Frame):
def __init __(self):
wx.Frame .__ init __(self,None)
blah = CustomWidget(self)
self .Show(True)
类CustomWidget(wx.PyControl):
def __init __(自己,父母):
wx.PyControl .__ init __(自己,父母=父母)
text = wx.TextCtrl(parent = self)
spin = wx.SpinButton(parent = self,style = wx.SP_VERTICAL)
sizer = wx.GridBagSizer()
self。 layout(text,spin,sizer)
self.OnInit(text,sizer)
def OnInit(self,text,sizer):
text.SetValue(u 0.000 )
def布局(self,text,spin,sizer):
self.SetSizer(sizer)
sizer.Add(text,pos = {0,0),flag = wx.ALIGN_CENTER)
sizer.Add(spin,pos =(0,1),flag = wx.ALIGN_CENTER)
self.Fit()
self.Layout()
self.CenterOnParent()
app = wx.A pp()
f = Frame()
app.MainLoop()
这样,如果您可以像上面那样附加一个可运行的示例,将来会很不错:)
Can a wx.PyControl
contain a wx.Sizer
?
Note that what I am ultimately trying to do here (spinner with float values) is already answered in another question. I am particularly interested in layouting widgets within a wx.PyControl
, a skill which might prove useful if I come across a need to make my own custom widgets. I already read through CreatingCustomControls, but it didn't use sizers within the wx.PyControl
subclass.
Using my code below, my CustomWidget
just doesn't look right. I'm not yet doing a DoGetBestSize
because I think that applies to a wx.Sizer
acting on a widget. I am actually having a wx.Sizer
doing its thing inside a CustomWidget
.
Here is my code (without the event bindings between sub-widgets):
EDIT: Here is my corrected class code, thanks to Steven Sproat:
import wx
class CustomWidget(wx.PyControl):
def __init__(self, parent):
wx.PyControl.__init__(self, parent=parent, style=wx.NO_BORDER) # Style added.
text = wx.TextCtrl(parent=self)
spin = wx.SpinButton(parent=self, style=wx.SP_VERTICAL)
sizer = wx.GridBagSizer()
self.layout(text, spin, sizer)
self.OnInit(text, sizer)
def OnInit(self, text, sizer):
text.SetValue(u"0.000")
def layout(self, text, spin, sizer):
self.SetSizer(sizer)
sizer.Add(text, pos=(0, 0), flag=wx.ALIGN_CENTER)
sizer.Add(spin, pos=(0, 1), flag=wx.ALIGN_CENTER)
self.Fit()
self.Layout() # This is what I lacked. I needed to call .Layout()
self.CenterOnParent()
Yes, it can. You just need to call Layout() to tell the sizer to recalculate/layout its children.
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
blah = CustomWidget(self)
self.Show(True)
class CustomWidget(wx.PyControl):
def __init__(self, parent):
wx.PyControl.__init__(self, parent=parent)
text = wx.TextCtrl(parent=self)
spin = wx.SpinButton(parent=self, style=wx.SP_VERTICAL)
sizer = wx.GridBagSizer()
self.layout(text, spin, sizer)
self.OnInit(text, sizer)
def OnInit(self, text, sizer):
text.SetValue(u"0.000")
def layout(self, text, spin, sizer):
self.SetSizer(sizer)
sizer.Add(text, pos=(0, 0), flag=wx.ALIGN_CENTER)
sizer.Add(spin, pos=(0, 1), flag=wx.ALIGN_CENTER)
self.Fit()
self.Layout()
self.CenterOnParent()
app = wx.App()
f = Frame()
app.MainLoop()
By the way, it would be nice in the future if you could attach a runnable sample as above :)
这篇关于wxPython:wx.PyControl可以包含wx.Sizer吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!