basic question

here is my code to create a basic frame
from wxPython.wx import *
class MyFrame(wxFrame):
def init(self,parent,id,title):
wxFrame.init(self,parent,-1,title)

class MyApp(wxApp):
def OnInit(self):
frame = MyFrame(None,-1,“title”)

    frame.Show(true)

    self.SetTopWindow(frame)

    return true

app = MyApp(0)

app.MainLoop()

now I don’t get some stuff and I don’t find any information about them
the stuff I don’t understand are in bold

I just ask why you have 2 init things in that class and why all the
parameters

and what is parent and id ? and why do they mostly put id = -1 ?

···

Hi Jonas,

here is my code to create a basic frame

from wxPython.wx import *

class MyFrame(wxFrame):
   def __init__(self,parent,id,title):
       wxFrame.__init__(self,parent,-1,title)

This should probably be:

    def __init__(self,parent,id,title):
        wxFrame.__init__(self,parent,id,title)

or

    def __init__(self,parent,title):
        wxFrame.__init__(self,parent,-1,title)

The way you have this setup right now, id is never used. Now on to your
questions. Parent is typically None for Frames because they are top-level
objects, but for other wxWindow components the parent is the window that the
object is contained within. The id is just a number identifying the object
it is used primarily by the event system. One generally wants each object to
have its own id. Setting the id to -1 tells wxWindows to pick the next free
id and use that.

As to why there are two init things in the class, your example is actually
almost too simple to make this clear because your __init__ method does
nothing but call the __init__ of the superclass (wxFrame). Let me make just
a teeny bit more complex (I'll just add some primitive logging).

class MyFrame(wxFrame):
    def __init__(self,parent,id,title):
        print "Creating MyFrame object with parent", parent, "id", id, "and
title", title
        wxFrame.__init__(self,parent,id,title)
        print "Creation complete"

First off, MyFrame.__init__(...) is what is called when you create a new
MyFrame object, while wxFrame.__init__ is what is called when you create a
new wxFrame object. In order for MyFrame to work correctly as a wxFrame
object, the initialization code for the wxFrame needs to be called as well
as whatever code is in your __init__ function (in this case a couple of
print statements) and that's what the call to wxFrame.__init__ is about.
This is a standard pattern when subclassing, you often need to call the
__init__ method of the superclass for things to go right.

[Many languages call the superclass constructor automagically, which is
often convenient, but sometimes a huge pain in the neck. Overall, I prefer
the specific call that python requires].

I hope that helps some, feel free to ask more questions if that was too
obscure.

-tim

class MyApp(wxApp):
   def OnInit(self):
       frame = MyFrame(None,-1,"title")
       frame.Show(true)
       self.SetTopWindow(frame)
       return true

app = MyApp(0)
app.MainLoop()

now I don't get some stuff and I don't find any information about them the

stuff I don't understand are in bold

I just ask why you have 2 init things in that class and why all the

parameters

···

and what is parent and id ? and why do they mostly put id = -1 ?