pycharm使用pyqt5

本文详细介绍了如何使用PyQt5和PyQt5-tools进行GUI界面设计,包括安装PyQt5及其工具、配置PyCharm外部工具、使用QtDesigner创建布局文件并将其转换为Python代码,以及最终在Python中调用布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,安装PyQt5,命令为: pip install pyqt5
一般情况可以找到你的python安装目录,按住shift,再右键“在此处打开命令窗口”
在这里插入图片描述
输入命令
在这里插入图片描述
2,安装pyqt5-tools,命令为: pip install pyqt5-tools
PyQt5不再提供Qt Designer等工具,所以需要再安装pyqt5-tools
在这里插入图片描述
3、添加外部工具designer.exe

先确认该文件目录复制好该地址:G:\Python37\Lib\site-packages\pyqt5_tools\designer.exe, 在这里插入图片描述

打开Pycharm,进入设置,添加外部工具
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

设置QtDesigner和PyUIC
QtDesigner设置,作用:设置快捷路径,可以直接打开designer.exe
看图输入两个变量值
Program: G:\Python37\Lib\site-packages\pyqt5_tools\designer.exe
Working directory: $FileDir$
在这里插入图片描述

PyUIC设置,作用:可将.ui文件转化为.py文件
看图输入两个变量值
Program: G:\Python37\python.exe
Arguments: -m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Working directory: $FileDir$

在这里插入图片描述

4、开发布局文件
打开工具在这里插入图片描述
显示下图
在这里插入图片描述

随意新建个窗口

在这里插入图片描述

保存后会生成ui文件,保存至项目路径
在这里插入图片描述
.ui文件(不用自己写)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QDialogButtonBox" name="buttonBox">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>240</y>
     <width>341</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonBox</sender>
   <signal>accepted()</signal>
   <receiver>Dialog</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>248</x>
     <y>254</y>
    </hint>
    <hint type="destinationlabel">
     <x>157</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>buttonBox</sender>
   <signal>rejected()</signal>
   <receiver>Dialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>316</x>
     <y>260</y>
    </hint>
    <hint type="destinationlabel">
     <x>286</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

生成py文件
在这里插入图片描述

生成的.py文件(不用自己写)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'TEST_QT_FROM.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")

        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))


在这里插入图片描述

最后调用使用

import sys
import TEST_QT_FROM
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QDialog
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mUi_Dialog = QDialog()
    ui = TEST_QT_FROM.Ui_Dialog()
    ui.setupUi(mUi_Dialog)
    mUi_Dialog.show()
    sys.exit(app.exec_())

运行后结果:
在这里插入图片描述

### 配置和使用 PyQt5 的方法 #### 安装必要的依赖包 为了在 PyCharm使用 PyQt5 进行开发,首先需要安装 `pyqt5` 和其相关工具包。可以通过以下命令完成这些库的安装[^1]: ```bash pip install pyqt5 pip install pyqt5-tools pip install pyqt5designer ``` #### 在 PyCharm 中配置 Python 解释器 进入 PyCharm 设置界面,在项目的 Python 解释器部分添加所需的软件包。通过搜索并安装 `pyqt5` 和 `pyqt5-tools` 来确保环境支持 PyQt5 开发[^3]。 #### 添加外部工具以集成 Qt Designer 为了让开发者能够更方便地设计 UI 文件 (.ui),可以在 PyCharm 中添加外部工具来启动 Qt Designer。以下是具体的配置方式: - **程序**: `/usr/bin/designer` 或者 Windows 下对应的路径 (例如:`C:\Program Files\Python38\Lib\site-packages\pyqt5_tools\designer.exe`)。 - **参数**: `$FileName$` - **工作目录**: `$ProjectFileDir$`[^4] 这样可以快速打开 `.ui` 文件进行编辑。 #### 将 .ui 转换为 .py 文件 为了使 Python 可以读取由 Qt Designer 创建的 `.ui` 文件,需将其转换为 Python 代码形式。为此可添加一个新的外部工具来进行该操作: - **名称**: 自定义名字, 如 `PyUIC` - **程序**: 指向 Python 执行文件位置 (通常位于 `Python\Python38\python.exe`) - **实参**: `-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py` - **工作目录**: `$FileDir$`[^5] 执行此工具即可自动生成相应的 `.py` 文件供后续调用。 #### 处理资源文件(.qrc) 当项目涉及图像或其他静态资源时,可能需要用到 `.qrc` 文件管理它们。同样也需要创建一个外部工具来做这种类型的转换: - **名称**: `qrcTOpy` - **程序**: 查找 `pyrcc5.exe` 的确切路径(一般是在 Scripts 文件夹下)。 - **实参**: `$FileName$ -o $FileNameWithoutExtension$_rc.py` - **工作目录**: `$FileDir$` 以上步骤完成后,整个基于 PyQt5 的 GUI 应用开发流程就已准备完毕! --- ### 示例代码片段展示如何加载 ui 文件 下面是一个简单的例子说明怎样利用上述生成好的 python 类实例化窗口对象: ```python from PyQt5 import uic import sys from PyQt5.QtWidgets import QApplication, QMainWindow class MyWindow(QMainWindow): def __init__(self): super(MyWindow, self).__init__() uic.loadUi('your_ui_file.ui', self) if __name__ == '__main__': app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_()) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值