Use records of commonly used components and small methods in Ptqt5 (opening files, displaying pictures, adding pictures to the background)

usage habit

variable naming

When using components, it is best to add a suffix to each component individually
insert image description here

Basic use of each component

button

Related information: Introduction to various buttons in Chapter 7 of "Quickly Mastering PyQt5"

class Test_window(QtWidgets.QMainWindow,Test_UI.Ui_MainWindow):
    def __init__(self,parent=None):
        super(Test_window,self).__init__(parent)
        self.setupUi(self)

        self.pushButton_OpenFile.clicked.connect(self.button_test)

    def button_test(self):
        print(self.pushButton_OpenFile.isChecked())

Label

References: PyQt5 label centered image display (QLabel)

Common small functions

open a file or folder

basic method

1. Open a single file

QFileDialog.getOpenFileName() 

2. Multiple files open

QFileDialog.getOpenFileNames() 

3. Folder selection

QFileDialog.getExistingDirectory() 

4. File saving

QFileDialog.getSaveFileName() 

Example:

from PyQt5 import QtWidgets,QtCore
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtWidgets import QFileDialog
import sys

import Test_UI

class Test_window(QtWidgets.QMainWindow,Test_UI.Ui_MainWindow):
    def __init__(self,parent=None):
        super(Test_window,self).__init__(parent)
        self.setupUi(self)

        self.pushButton_OpenFile.clicked.connect(self.msg)

    def msg(self):
        directory1 = QFileDialog.getExistingDirectory(self,
                                                      "选取文件夹",
                                                      "./")  # 起始路径
        print(directory1)

        fileName1, filetype = QFileDialog.getOpenFileName(self,
                                                          "选取文件",
                                                          "./",
                                                          "All Files (*);;Text Files (*.txt)")  # 设置文件扩展名过滤,注意用双分号间隔
        print(fileName1, filetype)

        files, ok1 = QFileDialog.getOpenFileNames(self,
                                                  "多文件选择",
                                                  "./",
                                                  "All Files (*);;Text Files (*.txt)")
        print(files, ok1)

        fileName2, ok2 = QFileDialog.getSaveFileName(self,
                                                     "文件保存",
                                                     "./",
                                                     "All Files (*);;Text Files (*.txt)")



if __name__ == '__main__':
    app = QApplication(sys.argv)
    mytest = Test_window()
    mytest.show()
    app.exec_()

Open the file and display the image

Import the key header file QtGui

from PyQt5 import QtWidgets,QtCore,QtGui
class Test_window(QtWidgets.QMainWindow,Test_UI.Ui_MainWindow):
    def __init__(self,parent=None):
        super(Test_window,self).__init__(parent)
        self.setupUi(self)

        self.pushButton_OpenFile.clicked.connect(self.openimage)

    def openimage(self):
        imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
        jpg = QtGui.QPixmap(imgName).scaled(self.label_ShowImg.width(), self.label_ShowImg.height())
        self.label_ShowImg.setPixmap(jpg)

insert image description here

Insert background image qcc

Create a .qrc file

<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="/">
    <file>images/logo.png</file>

</qresource>
</RCC>

Then use PTQCC conversion, you can use these pictures directly in QtDesigner
insert image description here

Guess you like

Origin blog.csdn.net/qin_liang/article/details/130938164