Can someone explain the below code. As per my reading, Myframe inherits wx.frame but what I don’t understand is the below lines thats given in the init method,
super(MyFrame, self).__init__(parent, id, title,
pos, size, style, name)
Code
class MyFrame(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title="",
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.DEFAULT_FRAME_STYLE,
name="MyFrame"):
super(MyFrame, self).__init__(parent, id, title,
pos, size, style, name)
# Attributes
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour(wx.BLACK)
self.button = wx.Button(self.panel,
label="Push Me",
pos=(50, 50))
That bit of code simply allows you to use the parent class’s
__init__()method.So the attribute-assignment code does not have to be repeated in the child class for those particular attributes.
You could have also done this:
But then you are hard-coding the parent class name, and your code becomes less flexible.
Aside:
In Python 3 the call to
super()is simplified to: