P
P
pypyshka2017-03-10 11:45:14
Python
pypyshka, 2017-03-10 11:45:14

How to access an interface from another module?

Good afternoon.
Tell me, please, is it possible to access the program interface from another module, that is, like this:
main.py:

from PyQt5 import QtWidgets, QtGui, uic
import sys

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

    def load_main_window(self):
        uic.loadUi('resources/main.ui', self)
        self.setWindowTitle('Program')
        self.show()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_window = MainWindow()
    main_window.load_main_window()

    sys.exit(app.exec_())

This module, main.py, opens a window containing a label.
I tried to make another print_label.py module and create an instance of the class, so that later I can try to access the label:
from main import MainWindow

a = MainWindow()
a.label.setText('Test')

The point is that I wanted to import the print_label.py module in main.py and when main.py was launched, the text was displayed in the label. But, I feel, this is not how it is done, since when you run print_label.py or when it is imported into main.py, the program immediately exits without errors. What can be done in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tirael78, 2017-03-10
@pypyshka

It ends just the same with an error and a very serious one - you made a recursive call and the application crashed when it reached the recursion limit.
If you want to access an object from different modules, then either initialize it in a third module and import it, or pass it as an object reference.
This is so, offhand, without understanding why you need to do it this way.
In general, it makes sense for you to read about how to correctly untie components in an application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question