简体   繁体   English

PyQt5 未能导入 QtGui

[英]PyQt5 failing import of QtGui

I've just moved from PyQt4 to 5 and I'm having an issue with QtGui.我刚刚从 PyQt4 转移到 5,我遇到了 QtGui 的问题。 I installed using the 32bit windows installer, not my own build.我使用 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 QtGui work around but it still gives me the same然后我尝试根据Pyinstaller更改 sys.path :ImportError: cannot import name QtGui work 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.更新:看起来我确实导入了 QtGui,因为当我进入 IDLE 并尝试它时,它仍然会自动完成 QMovie 和一大堆其他属性。 Is there any reason QMainWindow just wouldn't be in there?有什么理由 QMainWindow 不会在那里吗? (It's not, neither is QDialog and they seem important) (不是,QDialog 也不是,它们似乎很重要)

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.主要的 GUI 元素在 QtWidgets 模块中,而更基本的 GUI 元素在 QtGui 中。 See the Qt modules page for more details.有关更多详细信息,请参阅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 之间的差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM