Pyqt realizes file browsing

Time:2021-1-13

background

Course design gui needs to be supplemented inPrevious articlesSimple picture reading software

realization

First, define a general button

self.bt2  =QPushButton ('browse ', self)
self.bt2.setGeometry(80, 90, 70 ,30) 
self.bt2 . settooltip ('< b > Click here to browse the file < / b >')

Link the button to the file browsing function

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

File browsing module

def setBrowerPath(self): 
    
    file_ path, file_ type =  QFileDialog.getOpenFileName (self, 'open file', '. /' ("images *. PNG *. XPM *. JPG)"))
    self.text.setText(file_path)
    self.text.selectAll()
    self.text.setFocus()

effect

Pyqt realizes file browsing

appendix

It seems that the folding function is not supported here. All of them are a little bloated outside. Let’s have a look

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 ('read picture ')
        self.setWindowIcon(QIcon('res/faye.jpg'))

        self.bt1  =QPushButton ('read ', self)
        self.bt1.setGeometry(150, 90, 70 ,30)
        self.bt1 . settooltip ('< b > Click here to read the picture < / b >')
        self.bt1.clicked.connect(self.showMessage)

        self.bt2  =QPushButton ('browse ', self)
        self.bt2.setGeometry(80, 90, 70 ,30)
        self.bt2 . settooltip ('< b > Click here to browse the file < / b >')
        self.bt2.clicked.connect(self.setBrowerPath)

        self.text  =Qlinedit ('input file path here (do not include Chinese),'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, 'little problem...','the path seems to be wrong ')
            self.text.selectAll()
            self.text.setFocus()
        else:
            cv2.imshow(filename, img)
            cv2.waitKey(0)
            self.text.clear()
            self.text.setText ('enter file path here (do not include Chinese))
            self.text.selectAll()
            self.text.setFocus()

    def setBrowerPath(self): 
        
        file_ path, file_ type =  QFileDialog.getOpenFileName (self, 'open file', '. /' ("images *. PNG *. XPM *. JPG)"))   
        self.text.setText(file_path)
        self.text.selectAll()
        self.text.setFocus()

    def closeEvent(self, event):

        reply =  QMessageBox.question (self, 'confirm', 'confirm exit'), QMessageBox.Yes  A kind of 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_())

finish

Recommended Today

Remote login of MySQL

MySQL remote login Here we take the windows platform as an example.When you do not configure any MySQL database and access MySQL remotely on another machine, you will report an error: Command line remote login mysql -h192.168.1.1 -uroot -p -P3306 1130 – Host ‘xxxx’ is not allowed to connect to this MySQL server At this […]