PyQt5 环境搭建+配置+怎样运行生成的.py程序 |
您所在的位置:网站首页 › 强劲怎么读音是什么读 › PyQt5 环境搭建+配置+怎样运行生成的.py程序 |
PyQt5 环境搭建+配置+怎样运行生成的.py程序
PyQt5安装及配置安装配置Error while finding module specification for 'PyQt5.uic.pyuic' (ModuleNotFoundError: No module named 'PyQt5')问题解决
建立ui界面生成.py程序并运行生成.py程序更改.py程序
效果后续
PyQt5安装及配置
安装
大家可以直接在pycharm的设置里下载pyqt相关的库,主要下载 sip,PyQt5,PyQt5-tools 配置打开PyCharm,打开File—>Settings—>External Tools,点击加号来添加自己的工具,做如下配置: pyuic配置 Name:Pyuic Group:Qt Program:D:\python\untitled1\venv\Scripts\python.exee(各位自己的python路径) Arguments:-m PyQt5.uic.pyuic F i l e N a m e FileName FileName -o F i l e N a m e W i t h o u t E x t e n s i o n FileNameWithoutExtension FileNameWithoutExtension.py Working directory: F i l e D i r FileDir FileDirPrograms后的地址是python地址 2. QTdesigner配置 3. Name:QtDesigner Group:Qt Programs:D:\python\untitled1\venv\Scripts\python.exe Working directory: P r o j e c t F i l e D i r ProjectFileDir ProjectFileDir Programs后的地址是python地址 Error while finding module specification for ‘PyQt5.uic.pyuic’ (ModuleNotFoundError: No module named ‘PyQt5’)问题解决出现这种问题的原因是,你引入的python.exe文件不是你这个工程的,出现这个问题说明你使用的pycharm,但你引入的是python.exe是python目录下的,你只需在上面的pyuic配置里,把python.exe地址改成pycharm你这个文件下的就行了 (实在找不到,就直接在文件夹里搜索) 建立ui界面首先在pycharm里打开QTdesigner 在QT中就可以创建你的界面了,这里自行发挥。 之后另存为到工程目录下,并更改为名称.ui,之后就可以在pycharm中看到了 生成.py程序并运行 生成.py程序右键刚才生成的.ui程序,点击pyuic 更改.py程序刚才生成的.py程序并不能生成窗口程序,我们还需要进行更改,这里进行简单的更改。 原来生成的代码如下: # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'untitled.ui' # # Created by: PyQt5 UI code generator 5.15.1 # # 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. from PyQt5 import QtCore, QtGui, 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.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(260, 220, 72, 15)) self.label.setObjectName("label") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) 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.label.setText(_translate("MainWindow", "JHKJGJKGK"))更改后的程序如下: import untitled # 需要运行的.py文件名 import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5 import QtCore, QtGui, 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.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(260, 220, 72, 15)) self.label.setObjectName("label") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) 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.label.setText(_translate("MainWindow", "JHKJGJKGK")) if __name__ == '__main__': app = QApplication(sys.argv) # 创建应用程序 mainwindow = QMainWindow() # 创建主窗口 ui = untitled.Ui_MainWindow() # 调用中的主窗口 ui.setupUi(mainwindow) # 向主窗口添加控件 mainwindow.show() # 显示窗口 sys.exit(app.exec_()) # 程序执行循环主要是在后面加上让他使用的主窗体并显示,还有就是一下库而已。 效果 后续如果想了解更多物联网、智能家居项目知识,可以关注我的项目实战专栏。 或者关注公众号。 编写不易,感谢支持。 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |