PyQt5 环境搭建+配置+怎样运行生成的.py程序

2020-12-2010:35:05后端程序开发Comments2,897 views字数 3589阅读模式

PyQt5安装及配置

安装

大家可以直接在pycharm的设置里下载pyqt相关的库,主要下载 sip,PyQt5,PyQt5-tools
PyQt5 环境搭建+配置+怎样运行生成的.py程序文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

配置

打开PyCharm,打开File—>Settings—>External Tools,点击加号来添加自己的工具,做如下配置:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

  1. 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 FileNameFileName -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 FileNameWithoutExtensionFileNameWithoutExtension.py
    Working directory:F i l e D i r FileDirFileDir

Programs后的地址是python地址文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

PyQt5 环境搭建+配置+怎样运行生成的.py程序
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 ProjectFileDirProjectFileDir文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

Programs后的地址是python地址文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

PyQt5 环境搭建+配置+怎样运行生成的.py程序文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

Error while finding module specification for ‘PyQt5.uic.pyuic’ (ModuleNotFoundError: No module named ‘PyQt5’)问题解决

出现这种问题的原因是,你引入的python.exe文件不是你这个工程的,出现这个问题说明你使用的pycharm,但你引入的是python.exe是python目录下的,你只需在上面的pyuic配置里,把python.exe地址改成pycharm你这个文件下的就行了
(实在找不到,就直接在文件夹里搜索)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

建立ui界面

首先在pycharm里打开QTdesigner
PyQt5 环境搭建+配置+怎样运行生成的.py程序
在QT中就可以创建你的界面了,这里自行发挥。
PyQt5 环境搭建+配置+怎样运行生成的.py程序
之后另存为到工程目录下,并更改为名称.ui,之后就可以在pycharm中看到了
PyQt5 环境搭建+配置+怎样运行生成的.py程序文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

生成.py程序并运行

生成.py程序

右键刚才生成的.ui程序,点击pyuic
PyQt5 环境搭建+配置+怎样运行生成的.py程序文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

更改.py程序

刚才生成的.py程序并不能生成窗口程序,我们还需要进行更改,这里进行简单的更改。
原来生成的代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

# -*- 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"))
复制代码

更改后的程序如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

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_())           # 程序执行循环
复制代码

主要是在后面加上让他使用的主窗体并显示,还有就是一下库而已。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

效果

PyQt5 环境搭建+配置+怎样运行生成的.py程序文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

作者:跋扈洋文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20753.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/20753.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定