<div dir="ltr"><div>Hi folks!</div>
<div> </div>
<div>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:</div>
<div> </div>
<div>Traceback (most recent call last):<br> File "textbox2TEST.py", line 36, in MainToSecond<br> MySecondFrame.text_ctrl_2.SetValue("This text was generated from the 'MainFrame' window")<br>
AttributeError: type object 'MySecondFrame' has no attribute 'text_ctrl_2'</div>
<div> </div>
<div>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! </div>
<div> </div>
<div>Here's the code that created the error:</div>
<div> </div>
<div> </div>
<div>import wx</div>
<div> </div>
<div>class MyMainFrame(wx.Frame):<br> def __init__(self, *args, **kwds):<br> # begin wxGlade: MyMainFrame.__init__<br> kwds["style"] = wx.DEFAULT_FRAME_STYLE<br> wx.Frame.__init__(self, *args, **kwds)<br>
self.text_ctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)<br> self.button_1 = wx.Button(self, -1, "Click me to bring up second window and write some text")</div>
<div> </div>
<div> self.__set_properties()<br> self.__do_layout()</div>
<div> </div>
<div> self.Bind(wx.EVT_BUTTON, self.MainToSecond, self.button_1)<br> # end wxGlade</div>
<div> </div>
<div> def __set_properties(self):<br> # begin wxGlade: MyMainFrame.__set_properties<br> self.SetTitle("Main Frame")<br> # end wxGlade</div>
<div> </div>
<div> def __do_layout(self):<br> # begin wxGlade: MyMainFrame.__do_layout<br> sizer_1 = wx.BoxSizer(wx.VERTICAL)<br> sizer_2 = wx.BoxSizer(wx.VERTICAL)<br> sizer_2.Add(self.text_ctrl_1, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)<br>
sizer_2.Add(self.button_1, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)<br> sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)<br> self.SetSizer(sizer_1)<br> sizer_1.Fit(self)<br> self.Layout()<br> # end wxGlade</div>
<div> </div>
<div> def MainToSecond(self, event): # wxGlade: MyMainFrame.<event_handler><br> MySecondFrame(self).Show()<br> MySecondFrame.text_ctrl_2.SetValue("This text was generated from the 'MainFrame' window")<br>
</div>
<div> </div>
<div># end of class MyMainFrame<br>class MySecondFrame(wx.Frame):<br> def __init__(self, *args, **kwds):<br> # begin wxGlade: MySecondFrame.__init__<br> kwds["style"] = wx.DEFAULT_FRAME_STYLE<br>
wx.Frame.__init__(self, *args, **kwds)<br> self.text_ctrl_2 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)<br> self.button_2 = wx.Button(self, -1, "Click me to close this frame and send some text back to the MainFrame")</div>
<div> </div>
<div> self.__set_properties()<br> self.__do_layout()<br> # end wxGlade</div>
<div> </div>
<div> def __set_properties(self):<br> # begin wxGlade: MySecondFrame.__set_properties<br> self.SetTitle("Frame Number Two")<br> # end wxGlade</div>
<div> </div>
<div> def __do_layout(self):<br> # begin wxGlade: MySecondFrame.__do_layout<br> sizer_3 = wx.BoxSizer(wx.HORIZONTAL)<br> sizer_4 = wx.BoxSizer(wx.VERTICAL)<br> sizer_4.Add(self.text_ctrl_2, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)<br>
sizer_4.Add(self.button_2, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)<br> sizer_3.Add(sizer_4, 1, wx.EXPAND, 0)<br> self.SetSizer(sizer_3)<br> sizer_3.Fit(self)<br> self.Layout()<br> # end wxGlade</div>
<div> </div>
<div># end of class MySecondFrame</div>
<div> </div>
<div>if __name__ == "__main__":<br> app = wx.PySimpleApp(0)<br> wx.InitAllImageHandlers()<br> frame_1 = MyMainFrame(None, -1, "")<br> app.SetTopWindow(frame_1)<br> frame_1.Show()<br> app.MainLoop()</div>
<div> </div></div>