Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt5 failing import of QtGui

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)

like image 507
Faller Avatar asked Dec 23 '13 19:12

Faller


People also ask

How do I import QtGui?

Enter the mainloop of application by app.Import QtCore, QtGui and QtWidgets modules from PyQt5 package. Create an application object of QApplication class. Add a QLabel object and set the caption of label as "hello world". Define the size and position of window by setGeometry() method.

Is pyqt4 compatible with PyQt5?

Versions. PyQt version 4 works with both Qt 4 and Qt 5. PyQt version 5 only supports Qt version 5, and drops support for features that are deprecated in Qt 5.

Is PyQt6 compatible with PyQt5?

The upgrade path from PyQt5 to PyQt6 is fairly straightforward, with one main gotcha. For some applications, just renaming the imports from PyQt5 to PyQt6 will be enough to convert your application to work with the new library. For most however, you will need to account for changes in both PyQt and Qt itself.

What is QApplication in Python?

QApplication specializes QGuiApplication with some functionality needed for QWidget -based applications. It handles widget specific initialization, finalization. For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time.


1 Answers

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.

like image 59
ekhumoro Avatar answered Sep 30 '22 14:09

ekhumoro