Pycharm Qt Designer的使用示例

Pycharm Qt Designer的使用示例

关于Qt Designer各组件的介绍可参考Qt Designer常用部件介绍,里面介绍了Qt Designer各部件的名字。
Qt Designer的使用很简单,直接根据自己所需把各部件拖入框内,更改各组件名称、大小、位置等。

例如:去除文件夹中模糊的图片
在这里插入图片描述
但是你会发现转换为py文件运行后并没有出现设计好的界面!
在这里插入图片描述
这时需要在该文件夹下重新建立一个py文件,例如main.py,输入以下代码并运行就会出现设计好的界面。这样就实现了界面和代码的分离。

from PyQt5 import QtWidgets
from remove import Ui_Form    #导入remove.py生成的类,Ui_Form为remove.py中类的名字

class mywindow(QtWidgets.QWidget, Ui_Form):
    def  __init__ (self):         #析构函数
        super(mywindow, self).__init__()
        self.setupUi(self)

if __name__=="__main__":
    import sys
    app=QtWidgets.QApplication(sys.argv)
    ui = mywindow()
    ui.show()
    sys.exit(app.exec_())

在本例中
Inputtext和Outputtext:LineEdit
InputButton和OutputButton:ToolButton
beginButton和closeButton:PushButton
在这里插入图片描述
点击InputButton和OutputButton都可以选择文件夹,然后点击“开始”即可如下图
在这里插入图片描述
在这里插入图片描述
完整代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: feifan time:2019/5/21
import sys
import cv2
import os
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
from PyQt5.QtGui import *
from remove import Ui_Form


class mywindow(QtWidgets.QWidget, Ui_Form):
    def __init__(self):
        super(mywindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle('图片筛选')
        self.InputButton.clicked.connect(self.openFile)     #点击按钮InputButton时调用openFile函数
        self.OutputButton.clicked.connect(self.saveFile)    # 点击按钮OutputButton时调用saveFile函数
        self.beginButton.clicked.connect(self.beginrun)     # 点击按钮beginButton时调用beginrun函数
        self.closeButton.clicked.connect(self.close)        # 点击按钮closeButton时调用内置方法关闭

    def makedir(path):  #新建文件夹
    if not os.path.exists(path):
        os.mkdir(path)
        
    def openFile(self):
        openfile_path = QFileDialog.getExistingDirectory(self, "请选择输入图片的路径", "/") #文件夹绝对路径
        self.Inputtext.setText(openfile_path)    #将文件夹路径显示在Inputtext文本框中

    def saveFile(self):
        savefile_path = QFileDialog.getExistingDirectory(self, "请选择输出图片的路径", '/') #文件夹绝对路径
        self.Outputtext.setText(savefile_path)    #将文件夹路径显示在Outputtext文本框中

    def beginrun(self):
        ImgPath = self.Inputtext.text()  #获取Inputtext中的内容即文件夹路径名
        SavePath =self.Outputtext.text()
        #makedir(SavePath)#如果没有该文件夹可新建
        THRESHOLD = 550.0   #像素阈值

        imagelist = os.listdir(ImgPath)   # 返回指定的文件夹包含的文件或文件夹的名字的列表。
        for imagee in imagelist:
            imgfile = ImgPath + '/' + imagee
            image = cv2.imread(imgfile);
            img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            imageVar = cv2.Laplacian(img2gray, cv2.CV_64F).var()  # 像素值
            if imageVar < THRESHOLD: continue
            savefile = SavePath + '/' + imagee
            cv2.imwrite(savefile, image)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ui = mywindow()
    ui.show()
    sys.exit(app.exec_())


注意:在使用Qt Desigher时应注意各组件相对应的方法,比如刚开始Inputtext和Outputtext处我用的是Text Edit组件,结果Text Edit组件没有相应的text()方法来获取文本内容,一直显示“进程已结束,退出代码-1073740791 (0xC0000409)”为此我找了很久的错误。。。。最后把Text Edit组件换成了Line Edit组件才能正常运行。所以你输入函数时一定要仔细看Pycharm给出的提示函数中是否有该函数!

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值