目录
1、对话框QDialog类别
2、通用对话框
编辑
3、消息对话框QMessageBox()
1)消息对话框QMessageBox类型
2)案例
编辑
4、输入对话框QInputDialog
1) 类型
2)案例
5、字体格式对话框QFontDialog
6、颜色对话框QColorDialog
1)获取颜色
2)利用颜色对话框修改字体颜色和背景色
①字体颜色设置
②字体背景色设置
③完整代码
注意:这种方法背景色和字体颜色无法同时修改
7、文件对话框
1)选择指定后缀名的文件
方法一
方法二
2)打开一个文件夹
3)利用QFileDialog实现图片选择和文件加载
1、对话框QDialog类别
QMessage——信息提示框
QColorDialog——颜色对话框
QFIleDialog——文件操作对话框
QFontDialog——字体格式对话框
QInputDialog——输入对话框
2、通用对话框
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/25 18:16
# @Author : @linlianqin
# @Site :
# @File : QDialog_learn.py
# @Software: PyCharm
# @description:from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys## 通用对话框
class QDialogDemo(QMainWindow):def __init__(self):super(QDialogDemo, self).__init__()self.InitUI()def InitUI(self):self.resize(300,100)self.setWindowTitle("Qdialog Demo")self.btn1 = QPushButton("弹出对话框",self)self.btn1.move(50,50)# 点击按钮弹出对话框self.btn1.clicked.connect(self.showDialog)# 创建对话框,点击按钮关闭def showDialog(self):dialog = QDialog()dialog.resize(200,100)dialog.setWindowTitle("对话框")btn = QPushButton("关闭",dialog) # 在dialog中创建按钮btn.move(50,50)btn.clicked.connect(dialog.close)# 将对话框设置为APP模式,即对话框未关闭时,没有办法对主窗口的控件进行操作# 进入app循环dialog.exec()if __name__ == '__main__':app = QApplication(sys.argv)mainWin = QDialogDemo()mainWin.show()sys.exit(app.exec_())
注:
1)当有多个窗口时,并且在不同的窗口创建控件时,需要指明其父类是谁
self.btn1 = QPushButton("弹出对话框",self)btn = QPushButton("关闭",dialog) # 在dialog中创建按钮2)创建一个新的对话框窗口时,不要忘记加上exec(),进入循环
dialog.exec()
3、消息对话框QMessageBox()
1)消息对话框QMessageBox类型
关于对话框——about
错误对话框——critical
警告对话框——warning
提问对话框——question
消息对话框——information
注:上述几种不同的消息对话框主要是显示的按钮不一样,且图标不一样
2)案例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/25 18:30
# @Author : @linlianqin
# @Site :
# @File : QMessageBox_learn.py
# @Software: PyCharm
# @description:from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys## 通用对话框
class QMessageBoxDemo(QWidget):def __init__(self):super(QMessageBoxDemo, self).__init__()self.InitUI()def InitUI(self):self.resize(300,100)self.setWindowTitle("Qdialog Demo")layout = QVBoxLayout()self.btn1 = QPushButton("关于对话框")self.btn2 = QPushButton("错误对话框")self.btn3 = QPushButton("警告对话框")self.btn4 = QPushButton("消息对话框")self.btn5 = QPushButton("提问对话框")layout.addWidget(self.btn1)layout.addWidget(self.btn2)layout.addWidget(self.btn3)layout.addWidget(self.btn4)layout.addWidget(self.btn5)# 点击按钮弹出对话框self.btn1.clicked.connect(self.showDialog)self.btn2.clicked.connect(self.showDialog)self.btn3.clicked.connect(self.showDialog)self.btn4.clicked.connect(self.showDialog)self.btn5.clicked.connect(self.showDialog)self.setLayout(layout)# 创建对话框,点击按钮关闭def showDialog(self):text = self.sender().text()if text == "关于对话框":QMessageBox.about(self,"关于","关于对话框")elif text == "错误对话框":QMessageBox.critical(self,"错误","错误对话框")if text == "警告对话框":QMessageBox.warning(self,"关于","关于对话框")if text == "消息对话框":QMessageBox.information(self,"消息","消息对话框")if text == "提问对话框":QMessageBox.question(self,"提问","提问对话框")# 消息对话框
if __name__ == '__main__':app = QApplication(sys.argv)mainWin = QMessageBoxDemo()mainWin.show()sys.exit(app.exec_())
4、输入对话框QInputDialog
1) 类型
QInputDialog.getItem——传入列表或者元组
QInputDialog.getText——录入文本
QInputDialog.getInt——录入整数
2)案例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/25 18:47
# @Author : @linlianqin
# @Site :
# @File : QInputDialog_learn.py
# @Software: PyCharm
# @description:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys## 输入对话框
class QInputDialogDemo(QWidget):def __init__(self):super(QInputDialogDemo, self).__init__()self.InitUI()def InitUI(self):self.resize(300,100)self.setWindowTitle("Qdialog Demo")layout = QFormLayout()self.btn1 = QPushButton("获取列表中的选项")self.btn1.clicked.connect(self.getItem)self.line1 = QLineEdit()layout.addRow(self.btn1,self.line1)self.btn2 = QPushButton("获取字符串")self.btn2.clicked.connect(self.getText)self.line2 = QLineEdit()layout.addRow(self.btn2,self.line2)self.btn3 = QPushButton("获取整数")self.btn3.clicked.connect(self.getInt)self.line3 = QLineEdit()layout.addRow(self.btn3,self.line3)self.setLayout(layout)def getItem(self):items = ["C","C++","Python"]item,ok = QInputDialog.getItem(self,"请选择编程语言","语言列表",items)if ok and item:self.line1.setText(item)def getText(self):text,ok = QInputDialog.getText(self,"文本输入框","输入姓名")if ok and text:self.line2.setText(text)def getInt(self):num,ok = QInputDialog.getInt(self,"整数输入框","输入年龄")if ok and num:self.line3.setText(str(num))if __name__ == '__main__':app = QApplication(sys.argv)mainWin = QInputDialogDemo()mainWin.show()sys.exit(app.exec_())
5、字体格式对话框QFontDialog
通过这个对话框可以设置字体类型、字体大小,返回的是一个元组,元组包含字体格式和一个bool类型
font,ok = QFontDialog.getFont()
font,ok = QFontDialog.getFont()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/26 10:14
# @Author : @linlianqin
# @Site :
# @File : QFontDialog_learn.py
# @Software: PyCharm
# @description:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys## 字体格式对话框
class QFontDialogDemo(QWidget):def __init__(self):super(QFontDialogDemo, self).__init__()self.InitUI()def InitUI(self):self.resize(300,100)self.setWindowTitle("QFontDialog Demo")layout = QVBoxLayout()self.btn1 = QPushButton("字体格式")self.label = QLabel("字体格式测试样例")self.btn1.clicked.connect(self.changeFont)layout.addWidget(self.btn1)layout.addWidget(self.label)self.setLayout(layout)def changeFont(self):font,ok = QFontDialog.getFont()if ok:self.label.setFont(font)if __name__ == '__main__':app = QApplication(sys.argv)mainWin = QFontDialogDemo()mainWin.show()sys.exit(app.exec_())
修改字体格式后:
6、颜色对话框QColorDialog
1)获取颜色
foreColor = QColorDialog.getColor()返回QColor类
2)利用颜色对话框修改字体颜色和背景色
①字体颜色设置
def setForeColor(self):# 通过颜色对话框得到颜色foreColor = QColorDialog.getColor()# 设置文字颜色p = QPalette()p.setColor(QPalette.WindowText,foreColor)self.label.setPalette(p)
②字体背景色设置
def setBgColor(self):# 设置背景色bgColor = QColorDialog.getColor()p = QPalette()p.setColor(QPalette.Window,bgColor)self.label.setAutoFillBackground(True)self.label.setPalette(p)
③完整代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/26 10:23
# @Author : @linlianqin
# @Site :
# @File : QColorDialog_learn.py
# @Software: PyCharm
# @description:from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys## 颜色对话框
class QColorDialogDemo(QWidget):def __init__(self):super(QColorDialogDemo, self).__init__()self.InitUI()def InitUI(self):self.resize(300,100)self.setWindowTitle("QColorDialog Demo")layout = QVBoxLayout()self.btn1 = QPushButton("设置字体颜色")self.btn2 = QPushButton("设置背景色")self.label = QLabel("颜色对话框测试")self.btn1.clicked.connect(self.setForeColor)self.btn2.clicked.connect(self.setBgColor)layout.addWidget(self.btn1)layout.addWidget(self.btn2)layout.addWidget(self.label)self.setLayout(layout)def setForeColor(self):# 通过颜色对话框得到颜色foreColor = QColorDialog.getColor()# 设置文字颜色p = QPalette()p.setColor(QPalette.WindowText,foreColor)self.label.setPalette(p)def setBgColor(self):# 设置背景色bgColor = QColorDialog.getColor()p = QPalette()p.setColor(QPalette.Window,bgColor)self.label.setAutoFillBackground(True)self.label.setPalette(p)if __name__ == '__main__':app = QApplication(sys.argv)mainWin = QColorDialogDemo()mainWin.show()sys.exit(app.exec_())
设置字体颜色
设置背景色
注意:这种方法背景色和字体颜色无法同时修改
7、文件对话框
PyQt5之QFileDialog_Anony吧的博客-CSDN博客_pyqt5 qfiledialogPyQt5之QFileDialogQFileDialog是用于打开和保存文件的标准对话框,QFileDialog在打开文件时使用了文件过滤器,用于显示指定扩展名的文件。也可以设置使用QFileDialog打开文件时的起始目录和指定扩展名的文件。一、QFileDialog类中的常用方法方法描述getOpenFileName()返回用户所选择的文件名称,并打开该文件getSaveFileName()使用用户选择的文件名保存文件setFilter()设置过滤器,只显https://blog.csdn.net/qq_44880255/article/details/106979791
QFileDialog是用于打开和保存文件的标准对话框,QFileDialog在打开文件时使用了文件过滤器,用于显示指定扩展名的文件。也可以设置使用QFileDialog打开文件时的起始目录和指定扩展名的文件。
# 选择后缀名为,jpg和.png的图片文件,并显示在label中 frame,_ = QFileDialog.getOpenFileName(self,"打开文件",".","Image files (*.jpg;*.png)")
1)选择指定后缀名的文件
方法一
def loadImage(self):# 选择后缀名为,jpg和.png的图片文件,并显示在label中frame,_ = QFileDialog.getOpenFileName(self,"打开文件",".","Image files (*.jpg;*.png)")print(frame)self.imageLabel.setPixmap(QPixmap(frame))
方法二
def loadText(self):dialog = QFileDialog()dialog.setFileMode(QFileDialog.AnyFile)dialog.setFilter(QDir.Files)if dialog.exec():filenames = dialog.selectedFiles()print(filenames)with open(filenames[0],encoding = "utf-8",mode='r') as f:data = f.read()self.contents.setText(data)
2)打开一个文件夹
def openDir(self):fname = QFileDialog.getExistingDirectory(self,"打开文件夹",".")self.DirLabel.setText("选择文件夹:"+fname)
3)利用QFileDialog实现图片选择和文件加载
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/26 10:57
# @Author : @linlianqin
# @Site :
# @File : QFileDialog_learn.py
# @Software: PyCharm
# @description:# 文件对话框
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sysclass QFileDialogDemo(QWidget):def __init__(self):super(QFileDialogDemo, self).__init__()self.InitUI()def InitUI(self):# self.resize(300)self.setWindowTitle("QFileDialog Demo")layout = QVBoxLayout()self.btn1 = QPushButton("加载图片")self.btn1.clicked.connect(self.loadImage)layout.addWidget(self.btn1)self.btn2 = QPushButton("加载文本")self.btn2.clicked.connect(self.loadText)layout.addWidget(self.btn2)self.btn3 = QPushButton("选择文件夹")self.btn3.clicked.connect(self.openDir)layout.addWidget(self.btn3)self.DirLabel = QLabel("文件夹名称")layout.addWidget(self.DirLabel)self.imageLabel = QLabel("----------")layout.addWidget(self.imageLabel)self.contents = QTextEdit()layout.addWidget(self.contents)self.setLayout(layout)def loadImage(self):# 选择后缀名为,jpg和.png的图片文件,并显示在label中frame,_ = QFileDialog.getOpenFileName(self,"打开文件",".","Image files (*.jpg;*.png)")print(frame)self.imageLabel.setPixmap(QPixmap(frame))def loadText(self):dialog = QFileDialog()dialog.setFileMode(QFileDialog.AnyFile)dialog.setFilter(QDir.Files)if dialog.exec():filenames = dialog.selectedFiles()print(filenames)with open(filenames[0],encoding = "utf-8",mode='r') as f:data = f.read()self.contents.setText(data)def openDir(self):fname = QFileDialog.getExistingDirectory(self,"打开文件夹",".")self.DirLabel.setText("选择文件夹:"+fname)if __name__ == '__main__':app = QApplication(sys.argv)mainWin = QFileDialogDemo()mainWin.show()sys.exit(app.exec_())
初始界面:
选择图像对话框:
选择文本对话框
选择文件夹对话框:
结果