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调用两个机器人聊天,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • python实现灰度图

    python实现灰度图

    这篇文章主要为大家详细介绍了python实现灰度图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • python实现跨年表白神器--你值得拥有

    python实现跨年表白神器--你值得拥有

    这篇文章主要介绍了python实现跨年表白神器的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-01-01
  • 如何用python 实现老板键功能

    如何用python 实现老板键功能

    这篇文章主要介绍了python 开发老板键功能的方法,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-03-03
  • python分析实现微信钉钉等软件多开分身

    python分析实现微信钉钉等软件多开分身

    我发现坛友分享的很多都是通过cmd 去start 多个微信,虽然能实现多开,但不够灵活,比如我上午登录了一个微信,下午在登录就不太好用了,当然也可能是我start的姿势不对。于是我就搜了下单实例原理,自己动手实现了个随用随开的
    2022-02-02
  • pycharm: 恢复(reset) 误删文件的方法

    pycharm: 恢复(reset) 误删文件的方法

    今天小编就为大家分享一篇pycharm: 恢复(reset) 误删文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Python爬取用户观影数据并分析用户与电影之间的隐藏信息!

    Python爬取用户观影数据并分析用户与电影之间的隐藏信息!

    看电影前很多人都喜欢去 『豆瓣』 看影评,所以我爬取44130条 『豆瓣』 的用户观影数据,分析用户之间的关系,电影之间的联系,以及用户和电影之间的隐藏关系,需要的朋友可以参考下
    2021-06-06
  • Python生成随机MAC地址

    Python生成随机MAC地址

    这篇文章主要介绍了Python生成随机MAC地址的相关资料,需要的朋友可以参考下
    2015-03-03
  • python 怎样进行内存管理

    python 怎样进行内存管理

    这篇文章主要介绍了python 是如何进行内存管理的,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2020-11-11
  • python爬取抖音视频的实例分析

    python爬取抖音视频的实例分析

    在本篇内容里小编给大家整理一篇关于python爬取抖音视频的实例分析的相关内容,有兴趣的朋友可以测试下实例内容。
    2021-01-01

最新评论