Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用;“自我”;在Python和wxPython中_Python_Wxpython - Fatal编程技术网

使用;“自我”;在Python和wxPython中

使用;“自我”;在Python和wxPython中,python,wxpython,Python,Wxpython,我目前正在阅读一本关于wxPython的流行出版物。在下面列出的用于创建两个不同wx.Frame子类的代码中,“self”的使用对我来说似乎很混乱和不一致。第一个代码示例中的变量前面有self,而第二个代码示例中的变量前面没有self。为什么选择使用self,何时不需要/不合适 class MouseEventFrame(wx.Frame): def __init__(self, parent, id): wx.Frame.__init__(self, parent, i

我目前正在阅读一本关于wxPython的流行出版物。在下面列出的用于创建两个不同wx.Frame子类的代码中,“self”的使用对我来说似乎很混乱和不一致。第一个代码示例中的变量前面有self,而第二个代码示例中的变量前面没有self。为什么选择使用self,何时不需要/不合适

class MouseEventFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Frame With Button',size=(300, 100))
        self.panel = wx.Panel(self)
        self.button = wx.Button(self.panel,label="Not Over", pos=(100, 15))
        self.Bind(wx.EVT_BUTTON, self.OnButtonClick,self.button)
        self.button.Bind(wx.EVT_ENTER_WINDOW,self.OnEnterWindow)
        self.button.Bind(wx.EVT_LEAVE_WINDOW,
        self.OnLeaveWindow)


class InsertFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Frame With Button',size=(300, 100))
        panel = wx.Panel(self)
        button = wx.Button(panel, label="Close", pos=(125, 10),size=(50, 50))
        self.Bind(wx.EVT_BUTTON, self.OnCloseMe, button)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

一般来说(这是对@roippi评论的重新表述),说
self.panel=…
使您可以稍后访问
self.panel
。它长期存储数据。但是如果您键入
panel=…
,您将无法在此方法之外访问名为
panel
的变量。

no self==<代码>自我。…==。第一个代码希望在这两个属性中保留对面板/按钮的直接引用,第二个代码则不这样做。没有比这更深的了。哦,好的。非常感谢。所以对于self.Bind(…)等方法,我假设self的意思不同?不,self的意思总是一样的。在任何好的python教程中都可以找到它。或者RTFM:谢谢你的帮助。所以,当在Bind中使用“self”时,我感觉它在说“在frame类的这个实例中,将这个绑定到那个”。对吗?当使用self.Bind时,Bind是从wx.Frame继承的方法。