0

I want to access MainWindow.blocks when Preview is being initialized. The code is as follows

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QListWidget, QHBoxLayout, QWidget, QFrame, QGridLayout, \
    QPlainTextEdit

from general.main import GeneralModule


# noinspection SpellCheckingInspection
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # Set-up MainWindow
        self.setWindowTitle("ORCAgui")
        self.mainwindow_layout = QHBoxLayout()

        # Add Preview
        self.preview = Preview(self)
        self.mainwindow_layout.addWidget(self.preview)

        # Setup Main Window view (QWidget)
        widget = QWidget()
        widget.setLayout(self.mainwindow_layout)
        self.setCentralWidget(widget)

        self.blocks = {
            'runtype': '',
            'method': '',
            'basis_set': '',
            'basis_block': '',
            'charge': '0',
            'multiplicity': '1',
            'coords': '',
        }

        self.show()

class Preview(QFrame):
    def __init__(self, parent):
        super().__init__(parent)

        # Set-up layout
        self.preview_layout = QGridLayout()

Someone adviced me to use self.parent().property but doesnt work.

When I run print(self.parent.block) the results is AttributeError: 'builtin_function_or_method' object has no attribute 'blocks'.

Moreover, running just print(self.parent) yields <built-in method parent of Preview object at 0x10bf3bca0>

Inserting help(self.parent) into def __init__(self, parent): of Preview yields:

Help on built-in function parent:

parent(...) method of __main__.Preview instance
    parent(self) -> QObject

Using self.parent().blocks results in AttributeError: 'MainWindow' object has no attribute 'blocks' and self.parent().preview results in AttributeError: 'MainWindow' object has no attribute 'preview'

  • 1
    It's not really clear why or what you want to do that but, since you need it for `Preview`, have you simply tried to move `self.blocks = ...` *before* `self.preview = Preview(self)`? – musicamante Jan 08 '22 at 21:15
  • And that was the solution! Simple yet so effective. Thank you! One more question - do you think it copies them or just pass as reference? –  Jan 08 '22 at 21:35

0 Answers0