Why no need to instantiate object in this sample?

Hello

I'm reading through Getting Started (http://wiki.wxpython.org). I
don't understand why the Form1 class is used directly, while other
objects first need to be instantiated as usual:

# ---------------------------- Sample 1
class MainWindow(wx.Frame):
  def __init__(self,parent,id,title):
    self.dirname=''
    wx.Frame.__init__(self,parent,wx.ID_ANY, title)
    self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)

app = wx.PySimpleApp()
frame = MainWindow(None, -1, "Sample editor")
app.MainLoop()

# ---------------------------- Sample 2
class Form1(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.quote = wx.StaticText(self, -1, "Your quote :",wx.Point
(20, 30))

app = wx.PySimpleApp()
frame = wx.Frame(None, -1, " Quote Control")
#How is Form created?
Form1(frame,-1)
frame.Show(1)
app.MainLoop()

I'd like to use wxPython to GUIfy a simple, text-based Python script
that takes a pushbutton and a label to display feedback, so this
little script looks like a good base to use.

Thank you.

Fred wrote:

Hello

I'm reading through Getting Started (http://wiki.wxpython.org). I
don't understand why the Form1 class is used directly, while other
objects first need to be instantiated as usual:

# ---------------------------- Sample 1
class MainWindow(wx.Frame):
  def __init__(self,parent,id,title):
    self.dirname=''
    wx.Frame.__init__(self,parent,wx.ID_ANY, title)
    self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)

app = wx.PySimpleApp()
frame = MainWindow(None, -1, "Sample editor")
app.MainLoop()

# ---------------------------- Sample 2
class Form1(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.quote = wx.StaticText(self, -1, "Your quote :",wx.Point
(20, 30))

app = wx.PySimpleApp()
frame = wx.Frame(None, -1, " Quote Control")
#How is Form created?
Form1(frame,-1)
  

The above line instantiates it, it just doesn't keep a reference to it and will be garbage collected as soon as the method this type of code is contained in is finished.

I would probably do it something like the attached (it is better to attach on this list, especially when things are a bit longer) shows - untested but that should give you the same result and allows you to easier use sizers.

Werner

fred1.py (790 Bytes)

Not quite true in this case. The C++ part of the wxPython window objects keep a reference to the Python proxy object. So the Python object will continue to exist as long as the C++ one does, which will be until the parent window is closed or destroyed, or the child is explicitly destroyed in some other way.

···

On 10/28/09 2:15 AM, werner wrote:

#How is Form created?
Form1(frame,-1)

The above line instantiates it, it just doesn't keep a reference to it
and will be garbage collected as soon as the method this type of code is
contained in is finished.

--
Robin Dunn
Software Craftsman