0

I am having difficulties in trying to get a function in a separate class. I have a main class with a few functions, one of which is reset:

class GUI(wx.Frame):
    [GUI STUFF HERE]

    def reset(self):
        self.data = [0]

Within that class i also have before the subroutines to initiate another class:

        self.controlPanel = controlPanel(self.panel)

Which initiates another class which is a staticbox with buttons. Within that class I have a function bound to a button event:

    def reset(self, event):
        GUI.reset()

where the function "reset" is in the main GUI class. I get an error when i try to call reset in the main class, yet I can do it the other way round. Why is this and how can I fix it? I want button events in child classes to call a function in the parent class.

Thanks in advance.

2
  • reset is an instance method, so to call it, you must supply an instance variable rather than just the class name. GUI.reset() won't work, but myGuiInstance.reset() would. Commented Feb 6, 2015 at 14:19
  • That has worked a treat, thank you so much! I had totally forgotten the instance that called the GUI class... Commented Feb 6, 2015 at 17:14

1 Answer 1

2

"GUI" is not defined in "controlPanel", you want to call the method of the instance of "GUI".

One way would be to do the following in your button handler:

self.GetParent().reset()

Depending how complex your application this might get out of hand as it will no longer work if you insert another layer in between GUI and controlPanel.

You might want to look into using 'wx.lib.pubsub' and in your controlPanel use 'pub.sendMessage' and in your GUI use 'pub.subscribe'.

wxPython Phoenix pubsub doc

pubsub's doc

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your input andsuggestion, I tried it and it said: AtrributeError: 'Panel' object has no attribute 'reset' I imagine its an issue on my end and I messed up somewhere (most likely!)... but Kevin has solved my issue.
@Cooper, hhm, probably my mistake as self.GetParent() is getting the parent of your button handler and GUI is at least one more level up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.