-1

In light of segregating/organising code, am lost on the best approach. The below works, with app.py imports after the MainWindow instantiation, but will that create subsequent problems, as I can't see how else things can be done (putting the imports after MainWindow is instantiated obviously gives 'main not found' from formatting.py).

app.py

from PyQt4.QtCore import *; from PyQt4.QtGui import *; from PyQt4.uic import *; from PyQt4.QtSql import *
app = QApplication(sys.argv)

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMaximumSize(500,500); self.showMaximized()

    def test(self):
        print self.sender()

main = Main()
main.show()

from ui.formatting import *

formatting.py

from PyQt4.QtCore import *; from PyQt4.QtGui import *; from PyQt4.uic import *; from PyQt4.QtSql import *
from app import main

class LineEdit(QLineEdit): 
    def __init__(self):
        QLineEdit.__init__(self)

        self.setParent(main)
        self.show()   

        self.textChanged.connect(main.test)


lineEdit = LineEdit()

Much appreciated

user2422819
  • 177
  • 13
  • 1
    You use in general too many global variables. Use more method parameters. Import statements then arrive almost automatically at the places you need them. Basically there is no reason for formatting.py to depend on main.py. See also http://stackoverflow.com/questions/193919/what-are-good-rules-of-thumb-for-python-imports or http://stackoverflow.com/questions/6011322/working-with-multiple-code-files-and-folders-in-python. – NoDataDumpNoContribution Jan 27 '15 at 22:25

1 Answers1

1

You should never rely on an object being instantiated when the import takes place (OK, there are a few times when you might want to do this, but it's very rare).

Instead, generally, you should use import statements to import modules, classes and/or functions. If an imported module/class/function requires access to something from another import (or from your main script) then you should pass that in explicitly when you use/instantiate/call said module/class/function.

So your example becomes:

formatting.py

from PyQt4.QtCore import *; from PyQt4.QtGui import *; from PyQt4.uic import *; from PyQt4.QtSql import *

class LineEdit(QLineEdit): 
    def __init__(self, main):
        QLineEdit.__init__(self)

        self.setParent(main)
        self.show()   

        self.textChanged.connect(main.test)

app.py

from PyQt4.QtCore import *; from PyQt4.QtGui import *; from PyQt4.uic import *; from PyQt4.QtSql import *
from ui.formatting import *

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMaximumSize(500,500); self.showMaximized()

    def test(self):
        print self.sender()


app = QApplication(sys.argv)
main = Main()
lineedit = LineEdit(main)
main.show()

Of course, this is a bit of a contrived example because it makes more sense to set the parent, and make the connection with your QLineEdit in app.py. Eg:

lineedit = LineEdit()
lineedit.setParent(main)
lineedit.textChanged.connect(main.test)

And then in that case, you don't need to subclass QLineEdit at all.

three_pineapples
  • 11,579
  • 5
  • 38
  • 75