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给出的提示函数中是否有该函数!
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 【华为云技术分享】漫谈LiteOS-端云互通组件-MQTT开发指南(上)
- 从安装PyQt5到打包成exe
- ubuntu 18.04 安装qt 4.7.4
- QT 初学(一)
- Qt5中配置MySQL
- Qt Creator快速入门_第三版(霍亚飞)学习中踩过的坑
- VS2008下qt连接VS2005方法
- QT 5.9 93页例子
- Qt编译报错:未找到文件main.obj
- Qt QSystenTrayIcon弹出Qmenu时不是位置偏移的问题
- VVC代码学习2:帧内亮度预测函数estIntraPredLumaQT
- 工厂有库存的我司高价IMX377CQT-C回收IMX317CQC-C长期回收OV芯片
- Qt+VS2015 关于exe文件的图标
- QT配置halcon&opencv(VS)
- Qt图像实时采集及边缘提取(多线程)
- Qt 连接SQL Server数据库出现的几点问题
- Failure to open file: /usr/lib/qt4/examples/dialogs/tabdialog/Makefile
- Qt读取wav文件处理音频数据时的格式转换问题
- Qt sqlite多条件查询插入QTableWidget表格(记录备份以防忘记,新手!)
- 搭建pyqt5环境