I have a program which is becoming rather sizable. I have a main app class Pierre
which encases all other major functions of the program and imports functions and classes from other scripts.
My main problem right now is that I have a widget that is separated into two classes and launched from inside the main app. The button that launches the utility window works fine, however a button inside the utility works about 50% of the time.
The following is the code structure:
In 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()
In 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
This structure of calling another class from a script just to prevent the main script from being bulky seems clumsy in itself. From my other research it seems like this causes the garbage collector to destroy the reference to the button.
How should this be structured to prevent garbage collection but keep the source of this program tidy and readable?