Friday 15 January 2010

Why it failed without import in python -


I am a newbie and I'm just starting to learn Python programming:

  import sys PyQt5 import QtWidgets if __name__ == "__main__": app = QtWidgets.QApplication (sys.argv) MainWindow = QtWidgets.QMainWindow () mainWindow.show () sys.exit (app.exec_ ())  

When I run the code above, then everything is fine. But when I run the code below, it fails with the following error message: Apps = PyQt5.QtWidgets.QApplication (sys.argv) AttributeError: 'module' object has no attribute 'QtWidgets'

  import sys import PyQt5 if __name__ == "__main__": app = PyQt5.QtWidgets.QApplication (sys.argv) MainWindow = PyQt5.Qtwidgets.QmainWindow () mainWindow.show () sys .exit (app .exec_ ())  

by the way, my Python version 2.7, I am using Qt5 library and my operating system OpenSUSE 13.2, of course, Distribution of Linux .

. It does not include anything yourself, and therefore you can not import anything directly from it.

This is an idea design decision, and is done for a good reason. There may be a total of thirty or more modules in the package, so they will have a heavy up-front cost to load all each time to import PyQt5 . So instead, you only have to pay the cost of loading the required module.

However, there may be times when you want to do load all the modules at once. For example, it would be very convenient to be able to do this when using the Python interactive session. And in fact, PyQt provides a special module that actually does this:

  & gt; & Gt; & Gt; Py Qt5 import from Qt & gt; & Gt; & Gt; Qt.QWidget & lt; Class 'PyQt5.QtWidgets.QWidget' & gt; & Gt; & Gt; & Gt; Qt.QObject & lt; Class 'PyQt5.QtCore.QObject' & gt; & Gt; & Gt; & Gt; Qt.QNetworkCookie & lt; Class 'PyQt5.QtNetwork.QNetworkCookie' & gt;  

No comments:

Post a Comment