Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How can I avoid (most likely due to a circular import) Error

How can I avoid (most likely due to a circular import) Error

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 549 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MAX001
    wrote on last edited by
    #1

    I have two files main.py and frame_functions.py. As I understand it, the error appears due to from 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.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 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")
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved