python中使用pyqt5出现 No module named 'QtWidgets'报错应该如何解决?
题目描述
在使用pyqt5创建窗口时我发现会一直报错 No module named 'QtWidgets'这个错误,网上搜了一下发现是pyqt5的版本问题,需要5.10的版本才支持这个模块,但我的清华源最早只有5.12.3的版本,我想问一下这个问题应该如何解决?以下是我的代码片段和相关的版本信息
题目来源及自己的思路
相关代码
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import sys
import QtWidgets as QtWidgets
# 程序入口,程序从此处启动PyQt设计的主窗口
def Ui_MainWindow():
pass
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow() # 创建窗口
ui = Ui_MainWindow() # 创建PyQt设计窗口
ui.setupUi(MainWindow) # 初始化设置
MainWindow.show() # 显示窗口
sys.exit(app.exec_()) # 程序关闭时退出进程
from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(230, 230, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
你期待的结果是什么?实际看到的错误信息又是什么?
回答:
是这样的吧,版本 5.15.7_1
import PyQt5.QtWidgets as QtWidgets
以上是 python中使用pyqt5出现 No module named 'QtWidgets'报错应该如何解决? 的全部内容, 来源链接: utcz.com/p/938653.html