1

I have a notebook whose parent is the main frame of the application. The main frame also has a panel showing a chart to the side of the notebook, a menu bar and a status bar.

The notebook has a couple of pages and each page has some nested panels.

I'd like the callbacks for buttons in those panels to be able to talk to the main frame.

At the moment, that means a ridiculous chain of 'parents'. For example, to get to status bar from a panel on a notebook page I would do:

stat = self.parent.parent.parent.status_bar

The first parent is the notebook page, the second parent is the notebook and finally the last parent is the main frame.

This leads to very obtuse code...

Naturally you can see how this might get worse if I wanted to talk between elements on the panel adjacent to the notebook or nest the notebook in it's own panel..

Any tips?

jramm
  • 6,415
  • 4
  • 34
  • 73

2 Answers2

0

There is a simple way to get your Main Frame.

Since you can get your app instance anywhere in your code with "wx.GetApp()", then you can set your Mainframe into your app instance, it would be easy to fecth.

Please try following simple sample:

import wx

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        btn = wx.Button(wx.Panel(self), label = "test")
        self.Bind(wx.EVT_BUTTON, self.onButton)

    def onButton(self, evt):
        print "onButton"
        app = wx.GetApp()
        print app.Myframe


app = wx.App()
frame = TestFrame()
frame.Center()
frame.Show()

app.Myframe = frame
app.MainLoop()
Jerry_Y
  • 1,724
  • 12
  • 9
0

If you need to get access to the top frame, you should be able to use wx.GetTopLevelParent(). Personally, I think pubsub is probably the easiest way to call various classes in wxPython and it's pretty clean too. Plus if you need to call multiple frames or panels or whatever, you can have them all "subscribe" to the same message name and then publish a message for all of them to pick up.

Here's a tutorial for pubsub: http://www.blog.pythonlibrary.org/2013/09/05/wxpython-2-9-and-the-newer-pubsub-api-a-simple-tutorial/

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88