Python PyQt5 未能导入 QtGui

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20749819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:09:16  来源:igfitidea点击:

PyQt5 failing import of QtGui

pythonpython-3.xpyqtpyqt5

提问by Faller

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.

我刚刚从 PyQt4 转移到 5,我遇到了 QtGui 的问题。我使用 32 位 Windows 安装程序进行安装,而不是我自己的构建。

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 QtGuiwork around but it still gives me the same

然后我尝试根据Pyinstaller更改 sys.path :ImportError: cannot import name QtGuiwork around 但它仍然给我相同的

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)

更新:看起来我确实导入了 QtGui,因为当我进入 IDLE 并尝试它时,它仍然会自动完成 QMovie 和一大堆其他属性。有什么理由 QMainWindow 不会在那里吗?(不是,QDialog 也不是,它们似乎很重要)

采纳答案by ekhumoro

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

假设一切都安装正确,您需要稍微调整导入以从 PyQt4 移植到 PyQt5。

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

主要的 GUI 元素在 QtWidgets 模块中,而更基本的 GUI 元素在 QtGui 中。有关更多详细信息,请参阅Qt 模块页面

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.

有关从 PyQt4 移植到 PyQt5 的更多详细信息,请参阅:PyQt4 和 PyQt5 之间的差异