Hello!! I desperately need help on event triggering across panels in wxPython! :(

I have 2 classes that inherit wx.Panel: Leftpanel & Rightpanel
Leftpanel consists of several buttons.
Rightpanel consists of a graph (i.e. using matplotlib).

I want to make the program so that pressing the button on Leftpanel will update(redraw) the graph on Rightpanel. (=event from pressing a button of a panel will trigger another function declared in another panel).

I have googled plenty of resources, but could not find any hints.
Any help would be greatly appreciated! Thanks! :)

Recommended Answers

All 3 Replies

sample code will help explain more. Also search for something like calling one method from another place in this forum.

What I know is you use their common parent as reference (SB can correct me if I'm wrong!)

Thanks for the reply. Here I'm attaching my code.
Explanation:
LeftPanel has a button which if pressed, will call the event handler on_button described in another class (RightPanel).

The question is:
How do you make a program which has one button in a panel(=left panel) which calls to a function in another panel(=right panel)?
The purpose of doing this is that I want to make a program that has 2 panels: one showing only buttons & textboxes and the other one showing a graph. Whenever the user changes a value in boxes/buttons in left panel, the graph in the right panel also changes.

When executing the program below I got an error:
TypeError: unbound method on_button() must be called with RightPanel instance as first argument (got CommandEvent instance instead)

Help would be greatly appreaciated! Thank you guys!:)

import wx

class RightPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

    def on_button(self, event):
        self.calculate()

    def calculate(self):
        # Another calculation here
        print 1+1

class LeftPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
  
        # A Button
        self.button = wx.Button(self, -1, 'Calculate!')

        # If the button is pressed, call on_button on the other class
        self.Bind(wx.EVT_BUTTON, RightPanel.on_button, self.button)

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Testing")
        self.left_panel = LeftPanel(self)
        self.right_panel = RightPanel(self)
        
        # Panel sizer
        self.panelsizer = wx.BoxSizer(wx.HORIZONTAL)
        self.panelsizer.Add(self.left_panel, 0, wx.LEFT | wx.TOP | wx.EXPAND)
        self.panelsizer.Add(self.right_panel, 1, wx.LEFT | wx.TOP | wx.EXPAND)
        self.SetSizer(self.panelsizer)
        self.panelsizer.Fit(self)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    app.frame = Frame()
    app.frame.Show()
    app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.