前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PyQt5 环境搭建+配置+怎样运行生成的.py程序

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

作者头像
跋扈洋
发布2021-02-02 13:57:18
1.5K0
发布2021-02-02 13:57:18
举报
文章被收录于专栏:物联网知识物联网知识

PyQt5安装及配置

安装

大家可以直接在pycharm的设置里下载pyqt相关的库,主要下载 sip,PyQt5,PyQt5-tools

配置

打开PyCharm,打开File—>Settings—>External Tools,点击加号来添加自己的工具,做如下配置:

  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地址

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 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程序并不能生成窗口程序,我们还需要进行更改,这里进行简单的更改。 原来生成的代码如下:

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

更改后的程序

代码语言:javascript
复制
import untitled    # 需要运行的.py文件名import sysfrom PyQt5.QtWidgets import QApplication, QMainWindowfrom PyQt5 import QtCore, QtGui, QtWidgetsclass 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_())           # 程序执行循环

主要是在后面加上让他使用的主窗体并显示,还有就是一下库而已。

效果

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 物联网知识 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • PyQt5安装及配置
    • 安装
      • 配置
        • Error while finding module specification for ‘PyQt5.uic.pyuic’ (ModuleNotFoundError: No module named ‘PyQt5’)问题解决
        • 建立ui界面
        • 生成.py程序并运行
          • 生成.py程序
            • 更改.py程序
              • 更改后的程序
              • 效果
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档