0

我确实在一本书中看到了这段代码,并引发了我关于继承的几个问题,

txt.py

class LoginPanel(wx.Panel):
    def __init__(self,parent)
        super(LoginPanel,self).__init__(parent)

        self._username = wx.TextCtrl(self)
        self._password = wx.TextCtrl(self,style=wx.TE_PASSWORD)

        sizer          = wx.FlexGridSizer(2,2,8,8)

        sizer.Add(wx.StaticText(self,label="Username:"),
                0,wx.ALIGN_CENTER_VERTICAL)
        sizer.Add(self._username,0,wx.EXPAND)
        sizer.Add(wx.StaticText(self,label="Password"),
                0,wx.ALIGN_CENTER_VERTICAL)
        sizer.Add(self._password,0,wx.EXPAND)

        msizer          = wx.BoxSizer(wx.VERTICAL)
        msizer.Add(sizer,1,wx.EXPAND|wx.ALL,20)
        btnszr          = wx.StdDialogButtonSizer()
        button          = wx.Button(self, wx.ID_OK)
        button.SetDefault()
        btnszr.AddButton(button)
        msizer.Add(btnszr,0,wx.ALIGN_CENTER|wx.ALL,12)
        btnszr.Realize()

        self.SetSizer(msizer)

我们如何直接使用作为 Window 对象一部分的 SetSizer 方法?那是我们如何在没有任何参考的情况下使用父方法吗?

4

1 回答 1

1

Any time you inherit from a class, it gets the methods of that class. If you were to import wx.Panel and do this:

dir(wx.Panel)

You'd get a whole bunch of methods and properties. If you did that same thing to your subclass, you'd get the same list plus whatever methods and properties you created in your subclass. And yes, you have to use "self.SomeMethod" to access the methods from the parent class AND for the ones you create yourself.

于 2012-08-28T19:16:00.867 回答