[Tutor] Is this a "Class" problem?

Adrian Greyling adrian.greyling at gmail.com
Mon Aug 18 18:13:35 CEST 2008

Hi folks!

I hope I'm in the right place to ask this question.  I'm new to Python and
have been working through some tutorials and have made it to the GUI
creation stage.  All I was hoping to do with the code below, was to open a
"secondary" window and have some text end up on a text_ctrl, but I get an
error message that looks like this:

Traceback (most recent call last):
  File "textbox2TEST.py", line 36, in MainToSecond
    MySecondFrame.text_ctrl_2.SetValue("This text was generated from the
'MainFrame' window")
AttributeError: type object 'MySecondFrame' has no attribute 'text_ctrl_2'

I'm using wxGlade and SPE together, so almost all of the code is generated
for me.  I just don't get why it doesn't work, although I think it has to do
with one class referencing another class, and I'm obviously not doing that
correctly...  Any help is much appreciated!

Here's the code that created the error:

import wx

class MyMainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyMainFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.text_ctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)
        self.button_1 = wx.Button(self, -1, "Click me to bring up second
window and write some text")


        self.Bind(wx.EVT_BUTTON, self.MainToSecond, self.button_1)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyMainFrame.__set_properties
        self.SetTitle("Main Frame")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyMainFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_2.Add(self.text_ctrl_1, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.button_1, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        # end wxGlade

    def MainToSecond(self, event): # wxGlade: MyMainFrame.<event_handler>
        MySecondFrame.text_ctrl_2.SetValue("This text was generated from the
'MainFrame' window")

# end of class MyMainFrame
class MySecondFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MySecondFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.text_ctrl_2 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)
        self.button_2 = wx.Button(self, -1, "Click me to close this frame
and send some text back to the MainFrame")

        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MySecondFrame.__set_properties
        self.SetTitle("Frame Number Two")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MySecondFrame.__do_layout
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_4 = wx.BoxSizer(wx.VERTICAL)
        sizer_4.Add(self.text_ctrl_2, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        sizer_4.Add(self.button_2, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        sizer_3.Add(sizer_4, 1, wx.EXPAND, 0)
        # end wxGlade

# end of class MySecondFrame

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    frame_1 = MyMainFrame(None, -1, "")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080818/91c4b6b1/attachment.htm>

More information about the Tutor mailing list