当前位置:网站首页>Pyqt5 displays file names and pictures

Pyqt5 displays file names and pictures

2022-07-05 03:55:00 *Program black*

The code is as follows

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class picture(QWidget):
    def __init__(self):
        super(picture, self).__init__()

        self.resize(600, 400)
        self.setWindowTitle("label display picture ")

        self.label = QLabel(self)
        self.label.setText("  display picture ")
        self.label.setFixedSize(int(410*2/3),int(410*2/3))
        self.label.move(160, 100)

        self.label.setStyleSheet("QLabel{background:white;}" "QLabel{color:rgb(300,300,300,120);font-size:15px;font-weight:bold;font-family: Song style ;}")

        btn = QPushButton(self)
        btn.setText(" Open the picture ")
        btn.move(10, 30)
        btn.clicked.connect(self.openimage)
        
        cbtn = QPushButton(self)
        cbtn.setText(" Select File ")
        cbtn.move(10, 60)
        cbtn.clicked.connect(self.changefiles)
        
        self.labels = QLabel(self)
        self.labels.setText('file ')
        self.labels.move(150, 60)
        
    def openimage(self):
        imgName, imgType = QFileDialog.getOpenFileName(self, " Open the picture ", "", "*.jpg;;*.png;;All Files(*)")
        print(imgName)
        jpg = QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height())
        self.label.setPixmap(jpg)
    
    def changefiles(self):
        # directory1 = QFileDialog.getExistingDirectory(self," Select a folder ","./") # Starting path 
        # print(directory1)

        filePath, filetype = QFileDialog.getOpenFileName(self, " Select file ", "./","All Files(*)")
        print(filePath)
        self.labels.setText(filePath)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my = picture()
    my.show()
    sys.exit(app.exec_())

Running results :
 Insert picture description here
Open the picture :
 Insert picture description here
Select File :
 Insert picture description here

原网站

版权声明
本文为[*Program black*]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140720406737.html