note: I have omitted irrelevant code to make this more concise
I'm trying to make 3 subclasses (TopUi, BottomLeftUi, and BottomRightUi) of a superclass (Ui), wherein the subclasses each have their own unique members that are explicitly called from a main.py file. I'm using this structure because I have data/members I want centralized to a single point rather than passed around to every new subclass.
However when I try to call methods from the subclasses, I get an error saying 'Ui' object has no attribute <x> Specifically,
Traceback:
File "main.py", line 81, in <module>
window = MainWindow()
File "main.py", line 20, in __init__
self.setup_ui()
File "main.py", line 39, in setup_ui
top_ui.setup_top_ui()
# UI.py
class Ui:
_instance = None
_initialized = False
# Shared data across all subclasses
_options = {}
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __getitem__(self, key):
return self._options.get(key)
def __setitem__(self, key, value):
self.options[key] = value
def __init__(self, event_controller):
if self.__class__._initialized:
return
self.__class__._initialized = True
self.options: dict[str, bool] = self._options
self.event_controller = event_controller
# ... Shared members here ... #
class TopUi(Ui)
def __init__(self, top_section, event_controller):
super().__init__(event_controller)
# ... Unique members here ... #
class BottomLeftUi(Ui):
def __init__(self, left_bottom_section, event_controller):
super().__init__(event_controller)
# ... Unique members here ... #
class BottomRightUi(Ui):
def __init__(self, right_bottom_section, event_controller):
super().__init__(event_controller)
# ... Unique members here ... #
# main.py
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.event_controller = EventControl() # custom event control class
self.ui_base_class = Ui(self.event_controller)
self.setup_ui()
def setup_ui(self):
self.top_section = QWidget()
top_ui = TopUi(self.top_section, self.event_controller)
top_ui.setup_top_ui()
self.left_bottom_section = QWidget()
left_bottom_ui = BottomLeftUi(self.left_bottom_section, self.event_controller)
left_bottom_ui.setup_bottom_left_ui()
self.right_bottom_section = QWidget()
right_bottom_ui = BottomRightUi(self.right_bottom_section, self.event_controller)
right_bottom_ui.setup_bottom_right_ui()