2013-12-23 373 views
17

我剛剛從PyQt4移動到5,我遇到了QtGui的問題。我使用32位Windows安裝程序安裝,而不是我自己的版本。PyQt5無法導入QtGui

當我這樣做:

from PyQt5 import QtGui 

我得到

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

所以我試圖

from PyQt5.QtWidgets import QtGui 

導致:

ImportError: cannot import name QtGui 

然後我試圖根據Pyinstaller: ImportError: cannot import name QtGui工作來改變周圍的sys.path但它仍然給了我同樣的

ImportError: cannot import name QtGui 

更新:它看起來像我其實進口QtGui做,因爲當我去在空閒和嘗試它,它仍然自動完成QMovie和一大堆其他屬性。 QMainWindow是否有任何理由不在? (它不是,QDialog也看起來很重要)

+0

建議使用Anaconda爲新手安裝開發環境。請[請按照我的文章](https://suiwenfeng.tk/pycharm-intigrated-with-pyqt5anaconda.html)一步一步。 – suiwenfeng

回答

27

假設一切安裝正確,您需要稍微調整導入以從PyQt4移植到PyQt5。

主要的GUI元素在QtWidgets模塊中,而更基本的GUI元素在QtGui中。有關更多詳細信息,請參閱Qt modules page。需要改變的東西像

的示例代碼:從PyQt4的移植到PyQt5

from PyQt5 import QtCore, QtGui, QtWidgets 

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

有關詳細信息,請參閱:Differences Between PyQt4 and PyQt5

+0

不勝感激,我花了太長的時間在這方面做出如此簡單的回答。 – Faller

+6

'QApplication'(例如'app = QtWidgets.QApplication(sys.argv)' 'app.exec _()')也已被移至QTWidgets。 – BenB