[pyqt5 learning]——Dialog QDialog learning (QMessageBox, QColorDialog, QFIleDialog, QFontDialog, QInputDialog)

Table of contents

1. Dialog QDialog category

2. Common dialog box

 Edit

3. Message dialog box QMessageBox() 

1) Message dialog box QMessageBox type

2) Case

 Edit

 4. Input dialog QInputDialog

1) Type

 2) Case

5. Font format dialog box QFontDialog

 6. Color dialog box QColorDialog

1) Get color

2) Use the color dialog box to modify the font color and background color

①Font color setting

 ②Font background color setting

③Complete code

Note: This method cannot modify the background color and font color at the same time. 

7. File dialog box

1) Select the file with the specified suffix name

method one

 Method Two

2) Open a folder

3) Use QFileDialog to implement image selection and file loading


1. Dialog QDialog category

QMessage——Message prompt box

QColorDialog——Color dialog box

QFIleDialog——File operation dialog box

QFontDialog - Font format dialog

QInputDialog——Input dialog box

2. Common dialog box

#!/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_())

 

 Note:

1) When there are multiple windows and when creating controls in different windows, you need to indicate who its parent class is.

self.btn1 = QPushButton("pop-up dialog box",self)
btn = QPushButton("Close",dialog) # Create button in dialog

2) When creating a new dialog window, don’t forget to add exec() and enter the loop

dialog.exec()

3. Message dialog box QMessageBox() 

1) Message dialog box QMessageBox type

About dialog box——about

Error dialog box - critical

Warning dialog box——warning

Question dialog box——question

Message dialog box——information

Note: The above-mentioned different message dialog boxes mainly display different buttons and icons.

2) Case

#!/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. Input dialog QInputDialog

1) Type

QInputDialog.getItem - pass in a list or tuple

QInputDialog.getText——Enter text

QInputDialog.getInt——Enter integers

 2) Case

#!/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. Font format dialog box QFontDialog

Through this dialog box, you can set the font type and font size. What is returned is a tuple, which contains the font format and a bool type.

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_())

 

After modifying the font format:

 

 6. Color dialog box QColorDialog

1) Get color

foreColor = QColorDialog.getColor()

Return QColor class

2) Use the color dialog box to modify the font color and background color

①Font color setting

	def setForeColor(self):
		# 通过颜色对话框得到颜色
		foreColor = QColorDialog.getColor()

		# 设置文字颜色
		p = QPalette()
		p.setColor(QPalette.WindowText,foreColor)
		self.label.setPalette(p)

 ②Font background color setting

	def setBgColor(self):
		# 设置背景色
		bgColor = QColorDialog.getColor()
		p = QPalette()
		p.setColor(QPalette.Window,bgColor)
		self.label.setAutoFillBackground(True)
		self.label.setPalette(p)

③Complete code

#!/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_())

 

Set font color

 

 Set background color

Note: This method cannot modify the background color and font color at the same time. 

7. File dialog box

QFileDialog of PyQt5_Anony Bar's Blog - CSDN Blog_pyqt5 qfiledialog QFileDialog of PyQt5QFileDialog is a standard dialog box for opening and saving files. QFileDialog uses a file filter when opening a file to display files with a specified extension. You can also set the starting directory and the file with a specified extension when using QFileDialog to open a file. 1. Common methods in the QFileDialog class. Method description: getOpenFileName() returns the file name selected by the user and opens the file. getSaveFileName() saves the file using the file name selected by the user. setFilter() sets the filter and only displays https://blog . .csdn.net/qq_44880255/article/details/106979791 

QFileDialog is a standard dialog box for opening and saving files. QFileDialog uses a file filter when opening a file to display files with a specified extension. You can also set the starting directory and the file with a specified extension when using QFileDialog to open a file. 

 

 

# Select image files with suffixes of jpg and .png and display them in the label 
frame,_ = QFileDialog.getOpenFileName(self,"Open file",".","Image files (*.jpg;*.png) ")

1) Select the file with the specified suffix name

method one

	def loadImage(self):
		# 选择后缀名为,jpg和.png的图片文件,并显示在label中
		frame,_ = QFileDialog.getOpenFileName(self,"打开文件",".","Image files (*.jpg;*.png)")
		print(frame)
		self.imageLabel.setPixmap(QPixmap(frame))

 Method Two

	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) Open a folder

	def openDir(self):
		fname = QFileDialog.getExistingDirectory(self,"打开文件夹",".")
		self.DirLabel.setText("选择文件夹:"+fname)

3) Use QFileDialog to implement image selection and file loading

#!/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 sys

class 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_())

Initial interface:

 Select image dialog:

Select text dialog

Select folder dialog:

 

 

 result

 

Guess you like

Origin blog.csdn.net/qq_45769063/article/details/124971172