简体   繁体   中英

wxPython cannot call function in main class from subclass

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.

"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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM