Pycharm Qt Designer的使用示例
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_())
#!/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()方法来获取文本内容,一直显示“进程已结束,退出代码- (0xC0000409)”为此我找了很久的错误。。。。最后把Text Edit组件换成了Line Edit组件才能正常运行。所以你输入函数时一定要仔细看Pycharm给出的提示函数中是否有该函数!
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/173712.html原文链接:https://javaforall.net
