[PyQt] From/Import, proper namespace,etc

Phil Thompson phil at riverbankcomputing.com
Sat May 19 09:59:37 BST 2012


On Fri, 18 May 2012 19:07:51 -0700 (PDT), JPolk <jpolk5001 at yahoo.com>
wrote:
> I think I figured this out once, lol...but can't seem to recall...
> Consider you have two python files,..."a" and "b"...where you "launch a"
> and
> "import b".
> But inside "b" is a PyQt call back to a control defined in "a".  I keep
> getting a "not defined" error...
> So,...
> 
> =======================================
> File "A"
> =======================================
> #!/usr/bin/env python
> 
> import os
> import sys
> from PyQt4 import QtCore, QtGui, uic
> 
> *from b import **
> 
> class MainWindow(QtGui.QMainWindow):
>         def __init__(self, parent=None):
>                 super(MainWindow, self).__init__(parent)
> 
>                 self.setGeometry(520, 285, 640, 480)
>                 self.Button = QtGui.QPushButton(self)
>                 self.Button.setGeometry(QtCore.QRect(105, 140, 151, 66))
>                 self.Button.setObjectName("Button")
>                 self.Label = QtGui.QLabel(self)
>                 self.Label.setGeometry(QtCore.QRect(350, 250, 101, 21))
>                 self.Label.setObjectName("Label")
>                 self.Button.setText("Test 1")
>                 self.Label.setText("AAA")
> 
>                 self.Button.clicked.connect(DoTest2)
> 
> def main():
>         app = QtGui.QApplication(sys.argv)
>         global form
>         form = MainWindow()
>         form.show()
>         app.exec_()
> 
> if __name__ == '__main__':
>         main()
> 
> 
> =====================================
> and File "B"
> =====================================
> 
> #!/usr/bin/env python
> 
> def DoTest2(self):
>         print("DoTest2")
>         *self.Label.setText("BBB")*
>         return("Done")
> 
> ======================================
> 
> 
> So,...to launch....
>           prompt>>  python a.py
> 
> Then, hit the button...the button calls a function in File-B, which
tries
> to
> modify text in a Label that
> is defined in File-A.....
> 
>      prompt -> python a.py
>      DoTest2
>      Traceback (most recent call last):
>        File "b.py", line 7, in DoTest2
>          self.Label.setText("BBB")
>      AttributeError: 'bool' object has no attribute 'Label'
> 
> So,..what's the proper way to do this?...I've tried every which way but
> nada...

    self.button.clicked(lambda checked: DoTest2(self))

...although a better answer might be "fix your design" :)

Phil


More information about the PyQt mailing list