PyQt5 failing import of QtGui

Faller picture Faller · Dec 23, 2013 · Viewed 71.2k times · Source

I've just moved from PyQt4 to 5 and I'm having an issue with QtGui. I installed using the 32bit windows installer, not my own build.

when I do:

from PyQt5 import QtGui

I get

class MainWindow(QtGui.QMainWindow, UI.MainUI.Ui_MainWindow):
AttributeError: 'module' object has no attribute 'QMainWindow'

so I tried

from PyQt5.QtWidgets import QtGui

Which results in:

ImportError: cannot import name QtGui

then I tried to change the sys.path according to Pyinstaller: ImportError: cannot import name QtGui work around but it still gives me the same

ImportError: cannot import name QtGui

Update: It looks like I do in fact import QtGui because when I go in IDLE and try it, it still autocompletes QMovie and a whole bunch of other attributes. Is there any reason QMainWindow just wouldn't be in there? (It's not, neither is QDialog and they seem important)

Answer

ekhumoro picture ekhumoro · Dec 23, 2013

Assuming everything was installed correctly, you need to adjust your imports slightly to port from PyQt4 to PyQt5.

The main GUI elements are in the QtWidgets module, whilst the more basic GUI elements are in QtGui. See the Qt modules page for more details.

The example code needs to be changed to something like:

from PyQt5 import QtCore, QtGui, QtWidgets

class MainWindow(QtWidgets.QMainWindow, UI.MainUI.Ui_MainWindow):
    ...

For more details on porting from PyQt4 to PyQt5, see: Differences Between PyQt4 and PyQt5.