您的位置:首页 > 编程语言 > Qt开发

Pycharm Qt Designer的使用示例

2020-02-17 05:27 567 查看

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:Text Edit
InputButton和OutputButton:Tool Button
beginButton和closeButton:Push Button

点击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给出的提示函数中是否有该函数!

  • 点赞
  • 收藏
  • 分享
  • 文章举报
qq_2533534326 发布了2 篇原创文章 · 获赞 15 · 访问量 4676 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: