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,打开,第一次配置时界面如下
然后按照箭头所示的操作进行
如果使用的是Anaconda,可以参考我的目录进行配置,如果找不到designer.exe就在python的安装目录搜一下
然后是配置pyUIC,这个工具的作用是将UI文件转换为.py文件
下面这个文件时打包用的,在命令行的用法是pyinstaller -F test.py
下面的工具是将ico图标放在qrc文件中,然后转为py文件,用于生成打包后的图标
按照上面的步骤都安装完成之后,可以在Tools中找到External Tools,然后里面就是我们刚刚创建的四个工具的快捷方式。
下面就是测试我们的设计工具,在项目文件中右键,会出来下面的菜单,在菜单中按照指示依次选择
然后会打开如下的界面,第一次打开就是这个样子,与qt Creator非常之像,简直就是一个模子刻出来的,用法也基本相同,但是没有右键控件转到槽函数的功能,哈哈哈。
我们选择widget,然后创建一个新的UI文件,然后选择保存,保存的位置就是你的pycharm工程目录
简单的设计下面一个界面
然后关掉设计界面,回到pycharm,可以看到目录中多了一个UI文件
在UI文件中我们右键,然后选择External Tools
这时对应UI的python代码就已经生成,可以看到目录中多了一个与UI文件同名的py文件
内容如下
# -*- 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_())
实验结果如下
来源:https://juejin.cn/post/7107603475545980942


猜你喜欢
- 网站或应用的登录页面有时候通常用户会看很多遍,同时也有机会诱使临时用户注册,所以,一个设计良好的登录页面会比你想象的更有用。这里是一些我们收
- 《Python for Data Analysis》GroupBy分组运算:split-apply-combine(拆分-应用-合并)Dat
- 使用到的函数是curl_init, curl_setopt, curl_exec,curl_close。默认是GET方法,可以选择是否使用H
- opencv > pilimport cv2 from PIL import Imageimg = cv2.imread("
- 一、常见模型分类1.1、循环服务器模型循环接收客户端请求,处理请求。同一时刻只能处理一个请求,处理完毕后再处理下一个。优点:实现简单,占用资
- JavaScript中的定时器大家基本在平时的开发中都遇见过吧,但是又有多少人去深入的理解其中的原理呢?下面我们就来分析一下定时器的实现原理
- 1.业务场景有如下树形结构: +—0 +—1 +—2 +—4 +—5 +—3如果删除某个父节点,则其子节点,以及其子节点的子节点,以此类推,
- 相信大家对街边林林总总的房产中介并不陌生,那么我们先看看下面这张图片。图1从右侧这家店的橱窗里,我们能迅速分清哪些是租房信息哪些是售房信息。
- 一、首先要确保你的电脑上opencv的环境和visual studio上的环境都配置好了,测试的时候通过了没有问题。二、那么只要在你项目里面
- 回车和换行的历史:机械打字机有回车和换行两个键作用分别是:换行就是把滚筒卷一格,不改变水平位置。 (即移到下一行,但不是行首,而是和上一行水
- 使用命令行登录MySQL报错1045 Access denied for user ‘root’@&
- 我就废话不多说了,大家还是直接看代码吧!def iou(y_true, y_pred, label: int): "&
- 一、MySQL修改密码方法总结首先要说明一点的是:一般情况下,修改MySQL密码是需要有mysql里的root权限的,这样一般用户是无法更改
- mysql表复制 &n
- 网站域名一般都会选简短易记的,因为这对于网站宣传来说也可以省不少力。而被很多网站忽视的站内Url结构则在一定程度上反映出网站的整体架构。当设
- VIM python下的一些关于缩进的设置:第一步: 打开终端,在终端上输入vim ~/.vimrc,回车。 第二步: 添加下面的文段:se
- 泡了论坛的艺术版块很长一段时间了,发现许多网站做不好,不对头的原因是在配色问题上,对于我来说,配色尤其重要,假如自己的绘图艺术能力不高,要突
- 1.什么是ORMORM 全拼Object-Relation Mapping.中文意为 对象-关系映射.在MVC/MVT设
- enumerate函数用于遍历序列中的元素以及它们的下标。enumerate函数说明:函数原型:enumerate(sequence, [s
- 前言JavaScript 中的 Infinity 是一个可以应用于任何变量的数值,表示无穷大。下面就来看看 Infinity 是如何工作的,