简体   繁体   English

我如何使班级互相交谈?

[英]How do I make classes talk to each other?

Here is the situation 这是情况

class App(wx.Frame):
    def __init__(self,parent,id=-1,title='Test App'):
        wx.Frame.__init__(self,parent,id,title,size=((1050,690)))

        self.mode = 'Test'


class SetModes:
    def __init__(self):
        self.GetModes = App(None,-1)

        self.appmode = ''

    def SetMode(self):
        self.appmode = self.GetModes.mode

Now this is just an example script because I already know this won't work but it shows what im essentially trying to do. 现在,这只是一个示例脚本,因为我已经知道这将无法正常工作,但是它显示了我本质上想做的事情。 Let me clarify: 让我澄清一下:

I want to get and set some variables from the app class. 我想从应用程序类中获取并设置一些变量。 I want to be able to do this from my 'SetModes' class. 我希望能够从“ SetModes”类中执行此操作。 Problem is I can't make a instance of it because I can't assign a parent to the instance so the self.GetModes method doesn't work, It tells me either the instance can't 'NOT' have a parent, So does this mean I can't create an instance of my App Class if all I want to do is use some of it's variables and functions??. 问题是我无法创建它的实例,因为我无法为该实例分配父对象,所以self.GetModes方法不起作用,它告诉我该实例不能“不”拥有父对象,所以这是否意味着如果我只想使用其中的某些变量和函数,就无法创建App类的实例??

So How do I get this to work. 所以我该如何工作。 Can I have a class communicate with a class that requires a parent? 我可以让班级与需要家长的班级进行交流吗?

I think part of your problem is that frames need to be initialized from inside a wx.App instance and then the MainLoop() must be called (see this tutorial . Note how the frame has a parent None ). 我认为您的问题的一部分是需要从wx.App实例内部初始化框架,然后必须调用MainLoop() (请参阅本教程 。请注意框架如何具有父项None )。

After that, a solution like jdi's will work. 之后,像jdi's这样的解决方案将起作用。 However, I still don't like using references if I can avoid it. 但是,如果可以避免的话,我仍然不喜欢使用引用。 For a parent calling functions on an immediate child, it's ok but my own personal rule is that my classes cannot see any objects they do not directly own (ie no knowledge about children of children). 对于在直接子代上调用函数的父代来说,可以,但是我个人的规则是,我的班级不能看到他们不直接拥有的任何对象(即,不了解孩子的子代)。

I'm a big fan of PubSub for communicating between unrelated classes. 我非常喜欢PubSub,因为它们可以在不相关的类之间进行通信。 Unlike wx.Event objects, there is no need for any heirarchical relationship. wx.Event对象不同,不需要任何层次关系。 Unlike the reference solution put forth by jdi, there is no need for classes to have any knowledge at all about each other's structures. 与jdi提出的参考解决方案不同,类不需要完全了解彼此的结构。

What it comes down to is, in your application, can a SetModes instance ever exist without an App instance? 归结为,在您的应用程序中,是否可以在没有 App实例的情况下存在SetModes实例? If no, can you maybe incorporate SetModes ' logic into App ? 如果没有,您是否可以将SetModes的逻辑合并到App If yes, then coding SetModes to explicitly deal with App is bad design. 如果是,则将SetModes编码为显式处理App是错误的设计。

Using PubSub , you send a request message from SetModes . 使用PubSub ,您可以从SetModes发送请求消息。 App sees the message and can then send a response message, which is seen by SetModes . App看到该消息,然后可以发送响应消息,该响应消息由SetModes看到。 Completely separate, completely asynchronous. 完全独立,完全异步。 SetModes doesn't need to know anything about App , just the message formats. SetModes不需要了解有关App任何信息,只需了解消息格式即可。

Just because the wx.Frame requires a parent, doesn't mean you can't assign a reference to it in another unassociated class. 仅仅因为wx.Frame需要一个父对象,并不意味着您不能在另一个未关联的类中为其分配引用。 Just parent your App instance to something as you would do normally, but pass the reference over to your SetModes: 像通常一样,将您的App实例作为父对象,然后将引用传递给SetModes:

app = App(parent)
setModes = SetModes()
setModes.GetModes = app

This comes down to a design issue with your program. 这归结为程序的设计问题。 Widgets need parents. 小部件需要父母。

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

相关问题 Python:彼此“交谈”的类 - Python: Classes that “talk” to each other 如何确定类不相等 - How to determine that classes do not equal each other 使两个python脚本相互交谈的最简单方法? - simplest way to make two python scripts talk to each other? 如何使这两个进程(程序)直接通过管道相互通信? - How can I get those two processes (programs) talk to each other directly using pipes? 如何通过Pygame 2.5使2张单独的图像互相追逐并互相跟随? - How do I make 2 separate images chase each other and follow each other via Pygame 2.5? 如何从两个不同文件中的两个不同类中获取函数以相互通信? - How do I get functions from two different classes in two different files to communicate with each other? Python - 如何在同一个文件中定义相互依赖的类? - Python - How do I get classes that rely on each other to be defined in the same file? 如何在 tkinter pyton 中制作相互接触的圆圈,并且它们的中心位于一个圆圈上? - How do I make circles that touch each other and their centers lies on one circle in tkinter pyton? 如何在主题之间用逗号使这些值彼此相邻 - How do I make theses values next to each other with commas in between theme 如何使用随机发生器使我的平台彼此远离 - How do I make my platforms a far distance away from each other using randomizer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM