Menu

#209 Make frame instance a class attribute of the app

closed
None
repository
2017-05-21
2017-05-21
No

The name of the frame instance generated is loca to the OnInit() function, therefore I can't reference the frame from outside the class (or the function).

class MyApp(wx.App):
    def OnInit(self):
        my_frame = MyFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(my_frame)
        my_frame.Show()
        return True

could/should be:

class MyApp(wx.App):
    def OnInit(self):
        self.my_frame = MyFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.my_frame)
        self.my_frame.Show()
        return True

This would allow access to the frame from outside the class. e.g. from the wxApp (or a derived wxApp) to then access one of the inner widets.

As an example if there is a text widget buried in the frame, and I want to use it (get or set data) from an an external asyncio coroutine/task then I need to obtain it via the hierachy. e.g. my_app.my_frame.my_text_ctrl.SetValue("new text")

There may be a better solution where the asyncio coroutine/task is a frame function. I need to investigate this further, but as a general rule it would be good to be able to access the frame from outside the class.

Discussion

  • Dietmar Schwertberger

    • status: open --> closed
    • assigned_to: Dietmar Schwertberger
    • Affected Version: --> repository
     
  • Dietmar Schwertberger

    Yes, I've stumbled over this problem this week myself when code like "MyFrame = MyFrame(...)" triggered a "used before assignment" error. I did consider adding a "global MyFrame" statement, but your suggestion certainly makes more sense. Thanks for this.
    Modified with commit 2336.
    (Please submit feature requests under the appropriate category.)

    Regards,
    Dietmar

     

Log in to post a comment.