乐趣区

关于python:PyQt实现文件浏览

背景

课程设计 GUI 须要,补充在之前文章的繁难图片读取软件上

实现

首先要定义一个惯例按钮

self.bt2 = QPushButton('浏览', self)
self.bt2.setGeometry(80, 90, 70 ,30) 
self.bt2.setToolTip('<b> 点击这里浏览文件 </b>')

将按钮链接到文件浏览性能中

self.bt2.clicked.connect(self.setBrowerPath)

文件浏览模块

def setBrowerPath(self): 
    
    file_path, file_type = QFileDialog.getOpenFileName(self, '关上文件','./',("Images (*.png *.xpm *.jpg)"))
    self.text.setText(file_path)
    self.text.selectAll()
    self.text.setFocus()

成果

附录

思否这边如同不反对折叠性能,全副在里面有点臃肿,凑活看吧

import sys
import cv2
import numpy
from PyQt5.QtWidgets import QFileDialog, QApplication, QWidget, QPushButton, QMessageBox, QLineEdit
from PyQt5.QtGui import QIcon
from random import randint

class Example(QWidget):

    def __init__(self):

        super().__init__()
        self.initUI()
        # self.filename = 'a.jpg'

    def initUI(self):

        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('读取图片')
        self.setWindowIcon(QIcon('res/faye.jpg'))

        self.bt1 = QPushButton('读取', self)
        self.bt1.setGeometry(150, 90, 70 ,30)
        self.bt1.setToolTip('<b> 点击这里读取图片 </b>')
        self.bt1.clicked.connect(self.showMessage)

        self.bt2 = QPushButton('浏览', self)
        self.bt2.setGeometry(80, 90, 70 ,30)
        self.bt2.setToolTip('<b> 点击这里浏览文件 </b>')
        self.bt2.clicked.connect(self.setBrowerPath)

        self.text = QLineEdit('在这里输出文件门路(不要含有中文)', self)
        self.text.selectAll()
        self.text.setFocus()
        self.text.setGeometry(25, 50, 250 ,30)

        self.show()

    def showMessage(self):

        filename = self.text.text()
        # print(filename)
        img = cv2.imread(filename)
        img1 = cv2.imread('a.jpg')
        
        if type(img) == type(img1):
            QMessageBox.about(self, '小问题...','门路如同错了')
            self.text.selectAll()
            self.text.setFocus()
        else:
            cv2.imshow(filename, img)
            cv2.waitKey(0)
            self.text.clear()
            self.text.setText('在这里输出文件门路(不要含有中文)')
            self.text.selectAll()
            self.text.setFocus()

    def setBrowerPath(self): 
        
        file_path, file_type = QFileDialog.getOpenFileName(self, '关上文件','./',("Images (*.png *.xpm *.jpg)"))   
        self.text.setText(file_path)
        self.text.selectAll()
        self.text.setFocus()

    def closeEvent(self, event):

        reply = QMessageBox.question(self, '确认', '确认退出吗', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()        
        else:
            event.ignore()  

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

退出移动版