避免垃圾收集的PyQt程序结构

时间:2018-07-07 02:03:48

标签: python oop pyqt pep

我的程序变得相当大。我有一个主要的应用程序类Pierre,其中包含了程序的所有其他主要功能,并从其他脚本中导入了功能和类。

我现在的主要问题是我有一个小部件,该小部件分为两个类并从主应用程序内部启动。启动实用程序窗口的按钮可以正常工作,但是实用程序内部的按钮大约有50%的时间可用。

以下是代码结构:

在pierre.py中:

from src.capsuleswindow import Ui_Capsules
from src import capsulecalc

class Pierre(Ui_PierreMainWindow):
    def __init__(self):
        super(Pierre, self).__init__()
        ...
        # additional initialization
        ...
        self.capsule.clicked.connect(self.open_capsules_utility)

     ...
     # additional functions of main program
     ...
    def open_capsules_utility(self):
        self.capsules_utility = CapsulesWindow()


class CapsulesWindow(QWidget):

    def __init__(self):
        super(CapsulesWindow, self).__init__()
        self.capsuleswindow = Ui_Capsules()
        self.capsuleswindow.setupUi(self)
        self.capsuleswindow.capsuleSize.setDisabled(True)
        capsule = capsulecalc.CapsuleFormula(self.capsuleswindow)
        self.capsuleswindow.calculate.clicked.connect(capsule.
                                                     validate_fields)
        self.show()

在capsulecalc.py中:

class CapsuleFormula:
     def __init__(self, capsules_window):
          self.capsules_window = capsules_window
          ...
          # additional initialization
          ...

     def validate_fields(self):
         # validation of user input text fields
         # this function is connected in pierre.py to its button
         # but only works half the time

从脚本调用另一个类只是为了防止主脚本笨拙的这种结构本身似乎很笨拙。从我的其他研究看来,这似乎导致垃圾收集器破坏了对该按钮的引用。

应如何构建结构以防止垃圾收集,但要使程序的源代码保持整洁和可读性?

0 个答案:

没有答案