简体   繁体   English

wxPython无法从子类调用主类中的函数

[英]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. 函数“ reset”在主GUI类中。 I get an error when i try to call reset in the main class, yet I can do it the other way round. 当我尝试在主类中调用reset时出现错误,但是我可以反过来做。 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". 在“ controlPanel”中未定义“ GUI”,您想调用“ 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. 根据您的应用程序的复杂程度,这可能会失控,因为如果在GUI和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'. 您可能希望使用'wx.lib.pubsub'进行研究,并在controlPanel中使用'pub.sendMessage',在GUI中使用'pub.subscribe'。

wxPython Phoenix pubsub doc wxPython Phoenix pubsub doc

pubsub's doc pubsub的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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