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

作者:是小峰呀 时间:2021-02-10 05:56:40 

之前一直使用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,打开,第一次配置时界面如下

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

简单的设计下面一个界面

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

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

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

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

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

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

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

内容如下

# -*- 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_())

实验结果如下

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

来源:https://juejin.cn/post/7107603475545980942

标签:pycharm,Qt,Designer
0
投稿

猜你喜欢

  • java.sql.SQLException: 内部错误: Unable to construct a Datum from the specified input

    2010-07-16 13:23:00
  • Python基础教程之异常详解

    2022-02-08 05:48:49
  • 探索Python数据可视化库中Plotly Express的使用方法

    2022-10-29 13:45:31
  • 用mysql做站点时怎样记录未知错误的发生

    2009-01-14 13:16:00
  • coreseek 搜索英文的问题详解

    2023-11-20 14:12:10
  • Laravel操作session和cookie的教程详解

    2023-06-01 21:28:50
  • python编写adb截图工具的实现源码

    2021-03-24 08:50:15
  • 键盘上下键的操作代码(选择)

    2008-06-10 12:28:00
  • Python3.8.2安装包及安装教程图文详解(附安装包)

    2021-11-17 19:35:14
  • SQL Server 总结复习(一)

    2012-10-07 11:04:02
  • python开发中range()函数用法实例分析

    2021-05-08 12:53:26
  • 不同浏览器的兼容一些写法

    2009-03-26 12:58:00
  • pytorch SummaryWriter保存日志的方法

    2023-07-24 22:07:31
  • 跟老齐学Python之集合的关系

    2023-02-04 22:32:27
  • asp_数据库操作封装

    2010-04-03 21:00:00
  • python内存动态分配过程详解

    2023-10-02 14:58:46
  • 使用css2.1实现多重背景、多重边框效果[译]

    2010-08-23 16:32:00
  • 一文带你搞懂Golang依赖注入的设计与实现

    2023-07-23 14:33:53
  • Python Django教程之实现天气应用程序

    2022-03-22 23:02:43
  • python 时间戳与格式化时间的转化实现代码

    2021-07-06 10:14:47
  • asp之家 网络编程 m.aspxhome.com