PyQt项目实战1

 转载

pyqt5:利用QFileDialog从本地选择图片\文本文档显示到label、保存图片\label文本到本地(附代码)_pyqt5中qfiledialog.getopenfileurl-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/tensixchuan/article/details/1057178331、QtDesigner的控件摆设完成后,可以定义函数设置其功能,再通过关联函数将控件与其对应的函数联系在一起。

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog, QApplication


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # pushButton_openImage
        self.pushButton_openImage = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_openImage.setGeometry(QtCore.QRect(60, 40, 75, 23))
        self.pushButton_openImage.setObjectName("pushButton_openImage")

        # pushButton_openImage
        self.pushButton_saveImage = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_saveImage.setGeometry(QtCore.QRect(60, 90, 75, 23))
        self.pushButton_saveImage.setObjectName("pushButton_saveImage")

        # pushButton_openImage
        self.pushButton_openDirectory = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_openDirectory.setGeometry(QtCore.QRect(60, 200, 75, 23))
        self.pushButton_openDirectory.setObjectName("pushButton_openDirectory")

        # pushButton_openFile
        self.pushButton_openFile = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_openFile.setGeometry(QtCore.QRect(60, 280, 75, 23))
        self.pushButton_openFile.setObjectName("pushButton_openFile")

        # pushButton_saveImage
        self.pushButton_saveFile = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_saveFile.setGeometry(QtCore.QRect(60, 340, 75, 23))
        self.pushButton_saveFile.setObjectName("pushButton_saveFile")

        self.label_imagePath = QtWidgets.QLabel(self.centralwidget)
        self.label_imagePath.setGeometry(QtCore.QRect(600, 80, 54, 12))
        self.label_imagePath.setObjectName("label_imagePath")
        self.label_directoryPath = QtWidgets.QLabel(self.centralwidget)
        self.label_directoryPath.setGeometry(QtCore.QRect(170, 210, 54, 12))
        self.label_directoryPath.setObjectName("label_directoryPath")
        self.label_filePath = QtWidgets.QLabel(self.centralwidget)
        self.label_filePath.setGeometry(QtCore.QRect(170, 290, 54, 12))
        self.label_filePath.setObjectName("label_filePath")

        self.label_txt = QtWidgets.QLabel(self.centralwidget)
        self.label_txt.setGeometry(QtCore.QRect(170, 339, 281, 161))
        self.label_txt.setStyleSheet("border: 1px solid black;")
        self.label_txt.setObjectName("label_txt")

        self.label_image = QtWidgets.QLabel(self.centralwidget)
        self.label_image.setGeometry(QtCore.QRect(170, 30, 281, 131))
        self.label_image.setStyleSheet("border: 1px solid black;")
        self.label_image.setObjectName("label_image")
        self.label_image.raise_()
        # self.label_image = QtWidgets.QLabel(self.centralwidget)
        # self.label_image.setGeometry(QtCore.QRect(200, 50, 321, 231))
        # self.label_image.setFrameShape(QtWidgets.QFrame.Box)
        # self.label_image.setObjectName("label_image")
        # self.label_image.setScaledContents(True)  # 图片填充整个框

        self.pushButton_openImage.raise_()
        self.pushButton_saveImage.raise_()
        self.pushButton_openDirectory.raise_()
        self.pushButton_openFile.raise_()
        self.pushButton_saveFile.raise_()
        self.label_imagePath.raise_()
        self.label_directoryPath.raise_()
        self.label_filePath.raise_()
        self.label_txt.raise_()

        MainWindow.setCentralWidget(self.centralwidget)

        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))

        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)

        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")

        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        # 关联函数
        self.pushButton_openImage.clicked.connect(self.openImage)
        self.pushButton_saveImage.clicked.connect(self.saveImage)
        self.pushButton_openFile.clicked.connect(self.openTextFile)
        self.pushButton_saveFile.clicked.connect(self.saveTextFile)
        self.pushButton_openDirectory.clicked.connect(self.openDirectory)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton_openImage.setText(_translate("MainWindow", "打开图像"))
        self.pushButton_saveImage.setText(_translate("MainWindow", "保存图像"))
        self.pushButton_openDirectory.setText(_translate("MainWindow", "打开文件夹"))
        self.pushButton_openFile.setText(_translate("MainWindow", "打开文件"))
        self.pushButton_saveFile.setText(_translate("MainWindow", "保存文件"))
        self.label_imagePath.setText(_translate("MainWindow", "图片路径"))
        self.label_directoryPath.setText(_translate("MainWindow", "文件夹路径"))
        self.label_filePath.setText(_translate("MainWindow", "文件路径"))
        self.label_txt.setText(_translate("MainWindow", "文本内容"))
        self.label_image.setText(_translate("MainWindow", "图片"))

    '''
    1.打开本地图片
    QFileDialog.getOpenFileName()返回值有两个,第一个是选择文件的文件名(文件路径),我们可以通过这个文件名打开本地文件,第二个是选择文件的类型
    有4个参数,第一个参数一般是self,第2个参数是弹出框的标题,第3个参数是打开对话框的默认地址,第4个参数是过滤字符串,一般用来限制文件类型,定义打开
    文件下拉框中的可选列表,并且不同的文件类型用两个分号;;隔开
    '''

    def openImage(self):  # 选择本地图片上传
        global imgName  # 这里为了方便别的地方引用图片路径,我们把它设置为全局变量
        imgName, imgType = QFileDialog.getOpenFileName(self.centralwidget, "打开图片", "",
                                                       "*.jpg;;*.png;;All Files(*)")  # 弹出一个文件选择框,第一个返回值imgName记录选中的文件路径+文件名,第二个返回值imgType记录文件的类型
        jpg = QtGui.QPixmap(imgName).scaled(self.label_image.width(),
                                            self.label_image.height())  # 通过文件路径获取图片文件,并设置图片长宽为label控件的长宽
        self.label_image.setPixmap(jpg)  # 在label控件上显示选择的图片 点击控件选择文件
        self.label_imagePath.setText(imgName)  # 显示所选图片的本地路径 就是大大的黑色方框

        '''
    2.保存图片到本地
    首先还是打开一个保存文件的弹出框,然后保存图片到刚刚选择的路径,这里保存有两种思路
    第一种是记录要保存图片原来的路径,获取到图片实例,然后把图片写入选择的路径
    第二种是把图片所在label控件屏幕截图,然后保存截图(我觉得这个比较简单)
    '''

    def saveImage(self):  # 保存图片到本地
        screen = QApplication.primaryScreen()
        pix = screen.grabWindow(self.label_image.winId())
        fd, type = QFileDialog.getSaveFileName(self.centralwidget, "保存图片", "", "*.jpg;;*.png;;All Files(*)")
        pix.save(fd)

    '''
    3.打开文件夹
    '''

    def openDirectory(self):  # 打开文件夹(目录)
        fd = QFileDialog.getExistingDirectory(self.centralwidget, "选择文件夹", "")
        self.label_directoryPath.setText(fd)

    '''
    4.打开本地文本文件并显示
    '''

    def openTextFile(self):  # 选择文本文件上传
        fd, fp = QFileDialog.getOpenFileName(self.centralwidget, "选择文件", "", "*.txt;;All Files(*)")
        f = open(fd, 'r')
        self.label_txt.setText(f.read())
        self.label_filePath.setText(fd)
        f.close()

    '''
    5.保存文本到本地
    '''

    def saveTextFile(self):  # 保存文本文件
        fd, fp = QFileDialog.getSaveFileName(self.centralwidget, "保存文件", "", "*.txt;;All Files(*)")
        f = open(fd, 'w')
        f.write(self.label_txt.text())
        f.close()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    formObj = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(formObj)
    formObj.show()
    sys.exit(app.exec_())

2.自己设置好后点击按钮没反应的原因是:

少了这一行代码:

from PyQt5.QtWidgets import QFileDialog, QApplication

你可能感兴趣的:(pyqt)