How can I avoid (most likely due to a circular import) Error
Moved
Unsolved
Qt for Python
-
I have two files
main.py
andframe_functions.py
. As I understand it, the error appears due tofrom main import window
in second file, but for each frame I want to change the window title. How can I better import the window and avoid the error?
main.pyimport sys from PyQt6.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog, QGridLayout from PyQt6.QtGui import QPixmap from PyQt6 import QtGui, QtCore, sip from PyQt6.QtGui import QCursor from frame_functions import frame1, frame2, grid if __name__ == "__main__": app = QApplication(sys.argv) window = QWidget() icon = QIcon() icon.addFile(u"Icons/Icon.png", QSize(), QIcon.Normal, QIcon.Off) window.setWindowIcon(icon) window.resize(1200, 900) window.setStyleSheet("background: #FFFFFF;") frame1() window.setLayout(grid) window.show() sys.exit(app.exec())
frames_functions.py
import sys from PyQt6.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog, QGridLayout from PyQt6.QtGui import QPixmap from PyQt6 import QtGui, QtCore, sip from PyQt6.QtGui import QCursor from main import window def frame1(): window.setWindowTitle("Project") def frame2(): window.setWindowTitle("Detection Algorithms")
-
Hi,
Well, the best way is to change your architecture. You are off to a really bad start doing it that way.
If you really want these functions in a different file, pass the "window" object as parameter of the function rather than trying to access objects created dynamically in different file through an import.