对话框应用
一、QMessageBox:对话框
1.对话框的种类
五种对话框
1.消息对话框
2.问答对话框
3.警告对话框
4.错误对话框
5.关于对话框
2.对话框的使用方法
QMessageBox.information(QWidget,'Title','Content',buttons,defaultbutton)
#QWidget:self或者窗口对象,表示该对话框所属的窗口
#Title:字符串,表示对话框的标题
#Content:字符串,表示对话框的内容
#buttons:对话框上要添加的按钮,多个按钮之间用“|”来连接,常见的按钮种类如表8.2,该值可选,没有制定该值时,默认为OK按钮
#defaultbutton:默认选中的按钮,该值可选,没有指定该值时,默认为第一个按钮
实例:弹出五种不同的对话框
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(638, 168)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(40, 50, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(140, 50, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(240, 50, 93, 28))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(340, 50, 93, 28))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(440, 50, 93, 28))
self.pushButton_5.setObjectName("pushButton_5")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 638, 26))
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.clicked.connect(self.info)
self.pushButton_2.clicked.connect(self.warn)
self.pushButton_3.clicked.connect(self.question)
self.pushButton_4.clicked.connect(self.critical)
self.pushButton_5.clicked.connect(self.about)
def info(self): #消息
QMessageBox.information(None,'消息','这是一个消息对话框',QMessageBox.Ok)
def warn(self): #警告
QMessageBox.warning(None,'警告','这是一个警告对话框',QMessageBox.Ok)
def question(self): #问答
QMessageBox.question(None,'问答','这是一个问答对话框',QMessageBox.Ok)
def critical(self): #错误
QMessageBox.critical(None,'错误','这是一个错误对话框',QMessageBox.Ok)
def about(self): #关于
QMessageBox.about(None,'关于','这是一个关于对话框')
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "消息框"))
self.pushButton_2.setText(_translate("MainWindow", "警告框"))
self.pushButton_3.setText(_translate("MainWindow", "问答框"))
self.pushButton_4.setText(_translate("MainWindow", "错误框"))
self.pushButton_5.setText(_translate("MainWindow", "关于框"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
3.与对话框进行交互
def info(self): #消息
select = QMessageBox.information(None,'消息','这是一个消息对话框',QMessageBox.Yes|QMessageBox.No)
if select == QMessageBox.Yes:
QMessageBox.information(MainWindow,'提醒','您同意了本次请求......')
二、QFileDialog:文件对话框
1.QFileDialog类概述
2.使用QFileDialog类选择文件
实例:选择并显示图片文件
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(557, 423)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
#创建一个按钮控件
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(10, 0, 93, 28))
self.pushButton.setObjectName("pushButton")
#listWidget列表
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(10, 30, 541, 341))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.bindList)
def bindList(self):
from PyQt5.QtWidgets import QFileDialog
dir = QFileDialog()
dir.setFileMode(QFileDialog.ExistingFiles)#多选
dir.setDirectory('C:\\')#默认C盘
dir.setNameFilter('图片文件(*.jpg *.png *.bmp *.ico *.gif)')#只显示图片文件
if dir.exec_():#是否选择了文件
self.listWidget.addItems(dir.selectedFiles()) #文件显示在列表中
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "选择文件"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
注:Python使用QFileDialog显示打开对话框时,还可以使用getOpenFileName()方法或getOpenFileNames()方法。
getOpenFileName():打开一个文件的文件名
getOpenFileNames():打开多个文件的文件名
替换:
def bindList(self):
from PyQt5.QtWidgets import QFileDialog
files,filetype = QFileDialog.getOpenFileNames(None,'打开','C:\\','图片文件(*.jpg *.png *.bmp *.ico *.gif)')
self.listWidget.addItems(files)
3.使用QFileDialog选择文件夹
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(586, 452)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(0, 0, 93, 28))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100, 0, 351, 31))
self.lineEdit.setObjectName("lineEdit")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(460, 0, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(10, 30, 541, 341))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.bindList)
def bindList(self):
from PyQt5.QtWidgets import QFileDialog
import os
dir = QFileDialog.getExistingDirectory(None,"选择文件夹路径",os.getcwd())
self.lineEdit.setText(dir)
list = os.listdir(dir)
self.listWidget.addItems(list) # 文件显示在列表中
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "选择路径:"))
self.pushButton_2.setText(_translate("MainWindow", "选择"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
三、QInputDialog:输入对话框
1.QInputDialog类概述
getText()方法
text,flag = QInputDialog.getText(QWidget,dlgTitle,txtLabel,echoMode,defaultInput)
getItem()方法
text,flag = QInputDialog.getItem(QWidget,dlgTitle,txtLabel,items,curIndex,editable)
getInt()方法
inputValue,flag = QInputDialog.getInt(QWidget,dlgTitle,txtLabel,defaultValue,minValue,maxValue,stepValue)
getDouble()方法
inputValue,flag = QInputDialog.getDouble(QWidget,dlgTitle,txtLabel,defaultValue,minValue,maxValue,decimals)
2.QInputDialog对话框的使用
实例:设计不同种类的输入框
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QInputDialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(621, 497)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100, 70, 141, 31))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(100, 120, 141, 31))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(100, 170, 131, 31))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(100, 230, 131, 31))
self.lineEdit_4.setObjectName("lineEdit_4")
#姓名
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 80, 72, 15))
self.label.setObjectName("label")
#年龄
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(60, 130, 72, 15))
self.label_2.setObjectName("label_2")
#班级
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(60, 180, 72, 15))
self.label_3.setObjectName("label_3")
#分数
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(60, 240, 72, 15))
self.label_4.setObjectName("label_4")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.lineEdit.returnPressed.connect(self.getname)
self.lineEdit_2.returnPressed.connect(self.getage)
self.lineEdit_3.returnPressed.connect(self.getgrade)
self.lineEdit_4.returnPressed.connect(self.getscore)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "录入学生信息"))
self.label.setText(_translate("MainWindow", "姓名:"))
self.label_2.setText(_translate("MainWindow", "年龄:"))
self.label_3.setText(_translate("MainWindow", "班级:"))
self.label_4.setText(_translate("MainWindow", "分数:"))
def getname(self):
#弹出可以输入字符串的输入框
name,ok = QInputDialog.getText(MainWindow,"姓名","请输入姓名",QtWidgets.QLineEdit.Normal,"王五")
if ok:
self.lineEdit.setText(name)
def getage(self):
age, ok = QInputDialog.getInt(MainWindow, "年龄", "请选择年龄", 20,1,100,1)
if ok:
self.lineEdit_2.setText(str(age))
def getgrade(self):
grade, ok = QInputDialog.getItem(MainWindow, "班级", "请选择班级", ('三年一班','三年二班','三年三班'),0,False)
if ok:
self.lineEdit_3.setText(grade)
def getscore(self):
score, ok = QInputDialog.getDouble(MainWindow, "分数", "请选择分数", 0.01,0,100,2)
if ok:
self.lineEdit_4.setText(str(score))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
相应文本框键入< Enter >键,弹出输入对话框
四、字体和颜色对话框
1.QFontDialog:字体对话框
QFontDialog表示选择字体大小、样式、格式等信息,类似word字体对话框
QFontDialog类中最常用的方法是getFont()方法,用来获取字体对话框中选择的字体相关的信息,其语法如下:
QFontDialog.getFont()
2.QColorDialog:颜色对话框
QColorDialog表示颜色对话框,用户可以从中选择颜色
QColorDialog最常用的方法是getColor()方法,用来获取在颜色对话框中选择的颜色信息,语法如下:
QColorDialog.getColor()
3.字体和颜色对话框的使用
实例:动态设置文本的字体和颜色
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFontDialog,QColorDialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(497, 491)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
#设置字体
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(10, 10, 93, 28))
self.pushButton.setObjectName("pushButton")
#设置颜色
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(120, 10, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
#水平布局
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 40, 431, 341))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
#标签
self.textEdit = QtWidgets.QTextEdit(self.horizontalLayoutWidget)
self.textEdit.setObjectName("textEdit")
self.horizontalLayout.addWidget(self.textEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.setfont)
self.pushButton_2.clicked.connect(self.setcolor)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "设置字体"))
self.pushButton_2.setText(_translate("MainWindow", "设置颜色"))
self.textEdit.setText(_translate("MainWindow","敢想敢为"))
def setfont(self):
font,ok = QFontDialog.getFont()
if ok:
self.textEdit.setFont(font)
def setcolor(self):
color = QColorDialog.getColor()
if color.isValid():
self.textEdit.setTextColor(color)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
选中设置颜色的文字