pycharm配置Qt Designer工具的图文教程

 更新时间:2023年06月05日 09:54:41   作者:是小峰呀  
本文主要介绍了pycharm配置Qt Designer工具的图文教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

之前一直使用QtCreator,在设计界面时非常方便,python早就集成了Qt模块,在python中以pyQt的包存在,目前常用的是pyQt5,但是在pycharm中却没有找到像QtCreator那样的编辑器,难道就只能通过代码进行界面编辑吗,那速度真是太慢了。不要慌,界面肯定是有的,而且非常方便,首先打开命令行安装pyqt包,命令如下

pip install PyQt5 -i https://pypi.doubanio.com/simple
pip install PyQt5-tools -i https://pypi.doubanio.com/simple
pip install paramiko -i https://pypi.doubanio.com/simple
pip install pyinstaller -i https://pypi.doubanio.com/simple

安装完了之后,下面就是在pycharm中配置qt designer

打开File,选择settings,然后找到External Tools,打开,第一次配置时界面如下

image.png

然后按照箭头所示的操作进行

image.png

如果使用的是Anaconda,可以参考我的目录进行配置,如果找不到designer.exe就在python的安装目录搜一下

image.png

然后是配置pyUIC,这个工具的作用是将UI文件转换为.py文件

image.png

下面这个文件时打包用的,在命令行的用法是pyinstaller -F test.py

image.png

下面的工具是将ico图标放在qrc文件中,然后转为py文件,用于生成打包后的图标

image.png

按照上面的步骤都安装完成之后,可以在Tools中找到External Tools,然后里面就是我们刚刚创建的四个工具的快捷方式。

image.png

下面就是测试我们的设计工具,在项目文件中右键,会出来下面的菜单,在菜单中按照指示依次选择

image.png

然后会打开如下的界面,第一次打开就是这个样子,与qt Creator非常之像,简直就是一个模子刻出来的,用法也基本相同,但是没有右键控件转到槽函数的功能,哈哈哈。

image.png

我们选择widget,然后创建一个新的UI文件,然后选择保存,保存的位置就是你的pycharm工程目录

简单的设计下面一个界面

image.png

然后关掉设计界面,回到pycharm,可以看到目录中多了一个UI文件

image.png

在UI文件中我们右键,然后选择External Tools

image.png

这时对应UI的python代码就已经生成,可以看到目录中多了一个与UI文件同名的py文件

image.png

内容如下

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'demo.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.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1066, 796)
        self.horizontalLayoutWidget = QtWidgets.QWidget(Form)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 1051, 80))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.openimg = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.openimg.setObjectName("openimg")
        self.horizontalLayout.addWidget(self.openimg)
        self.detect = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.detect.setObjectName("detect")
        self.horizontalLayout.addWidget(self.detect)
        self.closewindow = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.closewindow.setObjectName("closewindow")
        self.horizontalLayout.addWidget(self.closewindow)
        self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Form)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(10, 90, 1051, 701))
        self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
        self.label.setObjectName("label")
        self.horizontalLayout_2.addWidget(self.label)
        self.retranslateUi(Form)
        self.openimg.clicked.connect(Form.shouImg)
        self.detect.clicked.connect(Form.edgeDetect)
        self.closewindow.clicked.connect(Form.close)
        QtCore.QMetaObject.connectSlotsByName(Form)
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.openimg.setText(_translate("Form", "打开图片"))
        self.detect.setText(_translate("Form", "边缘检测"))
        self.closewindow.setText(_translate("Form", "关闭窗口"))
        self.label.setText(_translate("Form", "show Image"))

然后通过自建main函数进行调用,代码如下

from demo import Ui_Form
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QMainWindow, QCheckBox
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtCore import *
from PyQt5.QtCore import pyqtSlot
import sys
import os
import cv2
import numpy as np
from PyQt5.QtGui import *
class MainWindow(QMainWindow, Ui_Form):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent=parent)
        self.setupUi(self)
    # 打开图片按钮槽函数
    def shouImg(self):
        filePath = self.getFileDir()
        img = cv2.imread(filePath[0][0])
        img_dis = QImage(img, img.shape[1], img.shape[0], QImage.Format_RGB888)
        # 加载图片,并设定图片大小
        img_dis = QPixmap(img_dis).scaled(int(img.shape[1]), int(img.shape[0]))
        width = img_dis.width()  ##获取图片宽度
        height = img_dis.height()  ##获取图片高度
        if width / self.label.width() >= height / self.label.height():  ##比较图片宽度与label宽度之比和图片高度与label高度之比
            ratio = width / self.label.width()
        else:
            ratio = height / self.label.height()
        new_width = int(width / ratio)  ##定义新图片的宽和高
        new_height = int(height / ratio)
        new_img = img_dis.scaled(new_width, new_height)  ##调整图片尺寸
        # img_dis = QPixmap(img_dis).scaled(int(img.shape[1]), int(img.shape[0]))
        self.label.setPixmap(new_img)
    # 获取文件地址函数
    def getFileDir(self):
        try:
            self.file_path = QFileDialog.getOpenFileNames(self, "select file", "./", "*.*")
        except Exception as e:
            print(e)
        return self.file_path
    def edgeDetect(self):
        # 自己发挥
        pass
if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

实验结果如下

image.png

到此这篇关于pycharm配置Qt Designer工具的图文教程的文章就介绍到这了,更多相关pycharm Qt Designer内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 一百多行python代码实现抢票助手

    一百多行python代码实现抢票助手

    一百多行python代码轻松实现抢票助手,十一出行不再愁!本文具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • python自动定时任务schedule库的使用方法

    python自动定时任务schedule库的使用方法

    当你需要在 Python 中定期执行任务时,schedule 库是一个非常实用的工具,它可以帮助你自动化定时任务,本文给大家介绍了python自动定时任务schedule库的使用方法,需要的朋友可以参考下
    2024-02-02
  • python高级特性简介

    python高级特性简介

    这篇文章主要介绍了python高级特性的相关内容,其中包括切片,迭代,列表生成式,生成器,迭代器,感兴趣的朋友可以了解下
    2020-08-08
  • Python通过paramiko远程下载Linux服务器上的文件实例

    Python通过paramiko远程下载Linux服务器上的文件实例

    今天小编就为大家分享一篇Python通过paramiko远程下载Linux服务器上的文件实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • python3批量删除豆瓣分组下的好友的实现代码

    python3批量删除豆瓣分组下的好友的实现代码

    下面小编就为大家带来一篇python3批量删除豆瓣分组下的好友的实现代码。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • Python实现的堆排序算法示例

    Python实现的堆排序算法示例

    这篇文章主要介绍了Python实现的堆排序算法,结合实例形式分析了堆排序的原理及Python定义与使用堆排序算法的相关操作技巧,需要的朋友可以参考下
    2018-04-04
  • argparse 模块简介

    argparse 模块简介

    argparse是一个用来解析命令行参数的 Python 库,它是 Python 标准库的一部分,基于 python 2.7 的stdlib 代码,这篇文章主要介绍了argparse 模块详解,需要的朋友可以参考下
    2023-02-02
  • python3中for循环踩过的坑记录

    python3中for循环踩过的坑记录

    这篇文章主要给大家介绍了python3中for循环踩坑的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • python 插入日期数据到Oracle实例

    python 插入日期数据到Oracle实例

    这篇文章主要介绍了python 插入日期数据到Oracle实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • 基于python实现图片转字符画代码实例

    基于python实现图片转字符画代码实例

    这篇文章主要介绍了基于python实现图片转字符画代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09

最新评论