简体   繁体   English

相互导入两个 python 模块

[英]Importing two python modules from each other

I have two modules one for the Main Menu lets call it MainMenu.py and another one for the game InGame.py , there is a button in each module that when clicked should take me to the other module:我有两个模块,一个用于主菜单,我们将其称为MainMenu.py ,另一个用于游戏InGame.py ,每个模块中都有一个按钮,单击时应将我带到另一个模块:

NOTE: I always run MainMenu.py first then i open InGame using the button, I just need a way to go back to MainMenu.py then again back to a new ran InGame, I tried using classes for both modules but It didnt work because i have circular dependencies between other modules and InGame.py注意:我总是先运行 MainMenu.py,然后使用按钮打开 InGame,我只需要一种返回 MainMenu.py 然后再次返回新运行的 InGame 的方法,我尝试对两个模块都使用类,但它没有用,因为我在其他模块和 InGame.py 之间有循环依赖关系

# MainMenu.py
if button_clicked:
    # run InGame.py

and

# InGame.py
if button_clicked:
    #run MainMenu.py

I tried directly importing each module at first which obviously didnt work, it would take me from MainMenu to InGame, back to MainMenu and stops there, pressing the button in MainMenu wont do anything.我一开始尝试直接导入每个模块,这显然不起作用,它会将我从 MainMenu 带到 InGame,回到 MainMenu 并停在那里,按下 MainMenu 中的按钮不会做任何事情。

then I tried:然后我尝试了:

# MainMenu.py
if button_clicked:
    if __name__ == "main":
        import InGame
    else:
        del InGame
        sys.modules.pop('InGame')
        import InGame

and

# InGame.py
if button_clicked:
    import MainMenu

But it also did nothing after going to MainMenu and trying to press the button to InGame, it just stops there.但是在转到 MainMenu 并尝试按下按钮进入 InGame 后它也没有做任何事情,它只是停在那里。

I am sure this means my design is a mess but I even tried changing the whole design with no success so im looking for an easier fix.我确信这意味着我的设计一团糟,但我什至尝试更改整个设计但没有成功,所以我正在寻找更简单的解决方案。

I believe what you are trying to do is discouraged, there are a number of alternative approaches you can take which will remove the need for such cyclic references.我相信不鼓励您尝试做的事情,您可以采取许多替代方法来消除对此类循环引用的需求。 The first immediate idea I have to solve your problem involves using an event driven design pattern.我必须解决您的问题的第一个直接想法是使用事件驱动的设计模式。 The general concept behind it is to have the MainMenu register an event that is called by your button.它背后的一般概念是让 MainMenu 注册一个由您的按钮调用的事件。 Your button does not need to know who is listening to the event but the main menu can still just as easily receive the message.您的按钮不需要知道谁在收听事件,但主菜单仍然可以轻松接收消息。

EDIT: The name was eluding me whilst typing the answer, its typically called the Observer Pattern.编辑:这个名字在我输入答案时一直在躲避我,它通常被称为观察者模式。

When you run into problems like these, it generally indicates bad design, Its good that you were able to recognise that its a bit messy.当您遇到此类问题时,通常表明设计不佳,您能够认识到它有点混乱,这很好。 I highly suggest looking into design patterns (if you dont know them already) even if they are in languages you dont know since the concept behind them is whats important.我强烈建议研究设计模式(如果你还不知道它们),即使它们使用你不知道的语言,因为它们背后的概念很重要。

In your case having something which mediates the flow between the two modules is the way to go, it will act as a parent in a sense and let you go between the two children在你的情况下,有一些东西可以调解两个模块之间的流动是要走的路,它会在某种意义上充当父母,让你在两个孩子之间穿行

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

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