【发布时间】:2020-05-25 20:43:09
【问题描述】:
我知道有很多类似的问题,但是:对于这种情况,我没有找到正确的提示。
概述
我正在编写一个 GUI (PyQt5)。有一个主类Ui_MainWindow() 和一个类Algorithm() 进行一些计算。
首先调用Ui_MainWindow(),然后调用实例Algorithm()。
我的想法: 在Ui_MainWindow() 中,我调用了一个在Algorithm() 中定义的方法,称为def update(self)。现在,在def update(self) 中,我想调用一个方法def move(self),它又在第一类Ui_MainWindow() 中。
我的问题:要由另一个类调用一个方法,我需要先实例化它,但我不能实例化一个类两次(在开始和第二个类中),因为那会抛出无穷无尽的错误。
示例:
class Algorithm():
def __init__(self, job_list):
self.job_list = job_list
def update(self):
main = Ui_MainWindow() # this is the problem, I can not do this twice
main.move()
class Ui_MainWindow():
def __init__(self, *args, **kwargs):
super(Ui_MainWindow, self).__init__(*args, **kwargs)
self.setupUi()
def setupUi(self):
""" random code """
def move(self):
""" random code """
if __name__ == "__main__":
main = Ui_MainWindow() # And I need it here.
main.show()
如果我的解释不清楚,请告诉我。
感谢您的帮助。
【问题讨论】: