网络编程
位置:首页>> 网络编程>> Python编程>> pycharm配置Qt Designer工具的图文教程

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

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

标签: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,打开,第一次配置时界面如下

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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com