(目录)


一、QFileDialog文件对话框控件

方法 说明
getOpenFileName() 获取一个打开文件的文件名
getOpenFileNames() 获取多个打开文件的文件名
getSaveFileName() 获取保存的文件名
getExistingDirectory() 获取一个打开的文件夹
setAcceptMode() 设置接收模式,取值如下。QFileDialog.AcceptOpen:设置文件对话框为打开模式,这是默认值;OFileDialog.AcceptSave:设置文件对话框为保存模式
setDefaultSuffix() 设置文件对话框中的文件名的默认后缀名
setFileMode() 设置可以选择的文件类型,取值如下。QFileDialog.FileMode.AnyFile:任意文件(无论文件是否存在)﹔QFileDialog.FileMode.ExistingFile:已存在的文件;QFileDialog.FileMode.ExistingFiles:已存在的多个文件;QFileDialog.FileMode.Directory:文件夹;QFileDialog.FileMode.DirectoryOnly:文件夹(选择时只能选中文件夹)
setDirectory() 设置文件对话框的默认打开位置
setNameFilter() 设置名称过滤器,多个类型的过滤器之间用两个分号分割(例如:所有文件(.);Python文件(.py))﹔而一个过滤器中如果有多种格式,可以用空格分割(例如:图片文件(.jpg .png.bmp))
setViewMode() 设置显示模式,取值如下。QFileDialog.Detail:显示文件详细信息,包括文件名、大小、日期等信息;QFileDialog.List:以列表形式显示文件名
selectedFile() 获取选择的一个文件或文件夹名字
selectedFiles() 获取选择的多个文件或文件夹名字

例子

from PyQt6 import QtCore, QtWidgets
import sys

from PyQt6.QtWidgets import QApplication, QPushButton, QFileDialog, QWidget


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(566, 438)
        self.pushButton = QtWidgets.QPushButton(parent=Form)
        self.pushButton.setGeometry(QtCore.QRect(210, 180, 75, 23))
        self.pushButton.setObjectName("pushButton")

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "选择文件"))


def selectFile():
    fd = QFileDialog()
    fd.setFileMode(QFileDialog.FileMode.ExistingFiles)  # 设置多选
    fd.setDirectory("C:\\")  # 设置初始化路径
    fd.setNameFilter("图片文件(*.jpg *.png *.bmp *.ico *.gif)")  # 设置只选择图片文件
    if fd.exec():  # 执行
        print(fd.selectedFiles())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = QWidget()
    ui = Ui_Form()
    ui.setupUi(window)

    pushButton: QPushButton = ui.pushButton
    pushButton.clicked.connect(selectFile)

    window.show()
    sys.exit(app.exec())

二、QInputDialog输入对话框控件

QInputDialog类的常用方法:

  • getText()的的参数及返回值
参数 说明
QWidget 父窗口对象
dglTitle QInputDialog的标题
txtLabel QInputDialog内部显示的文本
echoMode 文本编辑框内容的显示方式
返回值 一个元组,其中text表示文本编辑框内的字符串,flag表示是否正常返回
  • getItem()方法的参数及返回值
参数 说明
QWidget 父窗口对象
dglTitle QInputDialog的标题
txtLabel QlnputDialog内部显示的文本
items ComboBox组件的内容列表
curIndex 默认显示ComboBox 组件哪一个索引的内容
editable ComboBox组件是否可被编辑
返回值 一个元组,其中 text 表示从ComboBox下拉列表中选择的内容,flag表示是否正常返回
  • getInt()方法的参数及返回值
参数 说明
QWidget 父窗口对象
dglTitle QInputDialog的标题
txtLabel QInputDialog内部显示的文本
defaultValue SpinBox控件默认值
minValue SpinBox控件最小值
maxValue SpinBox控件最大值
stepValue SpinBox控件单步值
返回值 一个元组,其中 inputValue表示SpinBox中选择的整数值,flag表示是否正常返回
  • getDouble()方法的参数及返回值
参数 说明
QWidget 父窗口对象
dglTitle QInputDialog的标题
txtLabel QInputDialog内部显示的文本
defaultValue DoubleSpinBox控件默认值
minValue DoubleSpinBox控件最小值
maxValue DoubleSpinBox控件最大值
decimals DoubleSpinBox控件显示的小数点位数控制
返回值 一个元组,其中 inputValue表示DoubleSpinBox中选择的小数值,flag 表示是否正常返回
from PyQt6 import QtCore, QtWidgets
import sys

from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit, QInputDialog


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(267, 220)
        self.formLayoutWidget = QtWidgets.QWidget(parent=Form)
        self.formLayoutWidget.setGeometry(QtCore.QRect(20, 10, 231, 191))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
        self.formLayout.setContentsMargins(10, 10, 10, 0)
        self.formLayout.setHorizontalSpacing(10)
        self.formLayout.setVerticalSpacing(20)
        self.formLayout.setObjectName("formLayout")
        self.label = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label.setObjectName("label")
        self.formLayout.setWidget(
            0, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label
        )
        self.lineEdit = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.lineEdit.setObjectName("lineEdit")
        self.formLayout.setWidget(
            0, QtWidgets.QFormLayout.ItemRole.FieldRole, self.lineEdit
        )
        self.lineEdit_2 = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.formLayout.setWidget(
            1, QtWidgets.QFormLayout.ItemRole.FieldRole, self.lineEdit_2
        )
        self.label_2 = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(
            1, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_2
        )
        self.label_3 = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label_3.setObjectName("label_3")
        self.formLayout.setWidget(
            2, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_3
        )
        self.lineEdit_3 = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.formLayout.setWidget(
            2, QtWidgets.QFormLayout.ItemRole.FieldRole, self.lineEdit_3
        )
        self.label_4 = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label_4.setObjectName("label_4")
        self.formLayout.setWidget(
            3, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_4
        )
        self.lineEdit_4 = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.formLayout.setWidget(
            3, QtWidgets.QFormLayout.ItemRole.FieldRole, self.lineEdit_4
        )

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "姓名:"))
        self.label_2.setText(_translate("Form", "年龄:"))
        self.label_3.setText(_translate("Form", "班级:"))
        self.label_4.setText(_translate("Form", "分数:"))


def getName(formLayoutWidget, name_input):
    name, ok = QInputDialog.getText(
        formLayoutWidget, "姓名", "请输入姓名", QLineEdit.EchoMode.Normal, "python222"
    )
    if ok:
        name_input.setText(name)


def getGrade(formLayoutWidget, grade_input):
    grade, ok = QInputDialog.getItem(
        formLayoutWidget,
        "班级",
        "请选择班级",
        ("大一1班", "大一2班", "大一3班"),
        0,
        False,
    )
    if ok:
        grade_input.setText(grade)


def getAge(formLayoutWidget, age_input):
    age, ok = QInputDialog.getInt(formLayoutWidget, "年龄", "请选择年龄", 18, 1, 100, 1)
    if ok:
        age_input.setText(str(age))


def getScore(formLayoutWidget, score_input):
    score, ok = QInputDialog.getDouble(
        formLayoutWidget, "分数", "请选择分数", 98.5, 0, 100, 2
    )
    if ok:
        score_input.setText(str(score))


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = QWidget()
    ui = Ui_Form()
    ui.setupUi(window)

    formLayoutWidget = ui.formLayoutWidget
    name_input: QLineEdit = ui.lineEdit
    name_input.returnPressed.connect(lambda: getName(formLayoutWidget, name_input))

    grade_input: QLineEdit = ui.lineEdit_3
    grade_input.returnPressed.connect(lambda: getGrade(formLayoutWidget, grade_input))

    age_input: QLineEdit = ui.lineEdit_2
    age_input.returnPressed.connect(lambda: getAge(formLayoutWidget, age_input))

    score_input: QLineEdit = ui.lineEdit_4
    score_input.returnPressed.connect(lambda: getScore(formLayoutWidget, score_input))

    window.show()

    sys.exit(app.exec())

三、控件支持的图标

支持.png和.ico文件格式。 图标的获取可以到:阿里巴巴矢量图标库

四、QFontDialog字体对话框控件

import sys

from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import (
    QApplication,
    QWidget,
    QPushButton,
    QMessageBox,
    QLineEdit,
    QInputDialog,
    QFontDialog,
)
from FontDialog import Ui_Form


def selectFont():
    font, ok = QFontDialog.getFont()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = QWidget()
    ui = Ui_Form()
    ui.setupUi(window)

    pushButton: QPushButton = ui.pushButton
    pushButton.clicked.connect(selectFont)

    window.show()

    sys.exit(app.exec())

五、QColorDialog颜色对话框控件

import sys

from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import (
    QApplication,
    QWidget,
    QPushButton,
    QColorDialog,
)

from ColorDialog import Ui_Form


def selectColor():
    color, ok = QColorDialog.getColor()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = QWidget()
    ui = Ui_Form()
    ui.setupUi(window)

    pushButton: QPushButton = ui.pushButton
    pushButton.clicked.connect(selectColor)

    window.show()

    sys.exit(app.exec())