1 QDialog简介
QDialog继承自QWidget, 子类有 QColorDialog, QErrorMessage, QFileDialog, QFontDialog, QInputDialog, QMessageBox, QProgressDialog等
2 QMessageBox
1 常用方法
方法名 | 描述 |
---|---|
about | 弹出关于对话框 |
critical | 弹出严重错误对话框 |
information | 弹出消息对话框 |
question | 弹出问答对话框 |
warning | 弹出警告对话框 |
2 标准按钮类型:
类型 | 描述 |
---|---|
QMessageBox.Yes | 同意操作 |
QMessageBox.No | 取消操作 |
QMessageBox.Ok | 同意操作 |
QMessageBox.Cancel | 取消操作 |
QMessageBox.Abort | 终止操作 |
QMessageBox.Retry | 重试操作 |
QMessageBox.Ignore | 忽略操作 |
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys,os
from PyQt5.QtWidgets import *
class QDialogDemo(QWidget):
def __init__(self):
super(QDialogDemo, self).__init__()
self.resize(400,400)
self.setWindowTitle("消息对话框demo")
self.layout = QVBoxLayout(self)
self.about_btn = QPushButton("关于对话框")
self.critical_btn = QPushButton("严重错误对话框")
self.information_btn = QPushButton("消息对话框")
self.question_btn = QPushButton("提问对话框")
self.warning_btn = QPushButton("警告对话框")
self.layout.addWidget(self.about_btn)
self.layout.addWidget(self.critical_btn)
self.layout.addWidget(self.information_btn)
self.layout.addWidget(self.question_btn)
self.layout.addWidget(self.warning_btn)
self.about_btn.clicked.connect(self.on_about_btn_clicked)
self.critical_btn.clicked.connect(self.on_critical_btn_clicked)
self.information_btn.clicked.connect(self.on_information_btnclicked)
self.question_btn.clicked.connect(self.on_question_btn_clicked)
self.warning_btn.clicked.connect(self.on_warning_btn_clicked)
def on_about_btn_clicked(self):
QMessageBox.about(self, "标题", "关于对话框消息正文")
def on_critical_btn_clicked(self):
QMessageBox.critical(self, "标题", "严重错误对话框消息正文", QMessageBox.Yes | QMessageBox.No|QMessageBox.Ignore, QMessageBox.Yes)
def on_information_btnclicked(self):
QMessageBox.information(self, '标题', '消息对话框正文', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
def on_question_btn_clicked(self):
QMessageBox.question(self, "标题", "提问框消息正文", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
def on_warning_btn_clicked(self):
QMessageBox.warning(self, "标题", "警告框消息正文", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
test = QDialogDemo()
test.show()
sys.exit(app.exec_())
3 交互按钮
3 QFileDialog
4 QInputDialog
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys,os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class QInputDialogDemo(QWidget):
def __init__(self):
super(QInputDialogDemo, self).__init__()
self.resize(400,400)
self.setWindowTitle("QInputDialogDemo")
self.layout = QGridLayout(self)
self.layout.addWidget(QLabel("姓名: "), 0 , 0, 1,1 )
self.name_edit = QLineEdit()
self.layout.addWidget(self.name_edit, 0, 1, 1, 1)
self.layout.addWidget(QLabel("年龄: "), 1 , 0, 1,1 )
self.age_edit = QLineEdit()
self.layout.addWidget(self.age_edit, 1, 1, 1, 1)
self.layout.addWidget(QLabel("籍贯: "), 2 , 0, 1,1 )
self.nativeplace_edit = QComboBox()
self.nativeplace_edit.addItems(["北京","深圳","上海"])
self.layout.addWidget(self.nativeplace_edit, 2, 1, 1, 1)
self.layout.setRowStretch(3,1)
self.name_btn = QPushButton("姓名输入")
self.age_btn = QPushButton("年龄输入")
self.nativeplace_btn = QPushButton("籍贯输入")
self.layout.addWidget(self.name_btn, 4, 0, 1, 2)
self.layout.addWidget(self.age_btn, 5, 0, 1, 2)
self.layout.addWidget(self.nativeplace_btn, 6, 0, 1, 2)
self.name_btn.clicked.connect(self.on_name_btn_clicked)
self.age_btn.clicked.connect(self.on_age_btn_clicked)
self.nativeplace_btn.clicked.connect(self.on_nativeplace_btn_clicked)
def on_age_btn_clicked(self):
age, ok = QInputDialog.getInt(self, '数字输入框', '请输入年龄')
if ok and age:
self.age_edit.setText(str(age))
def on_nativeplace_btn_clicked(self):
items = ('北京', '上海', '深圳',"桃源")
item, ok = QInputDialog.getItem(self, '请选择籍贯', '籍贯列表', items)
if ok and item:
if -1 == self.nativeplace_edit.findText(item):
self.nativeplace_edit.addItem(item)
self.nativeplace_edit.setCurrentText(item)
else:
self.nativeplace_edit.setCurrentText(item)
def on_name_btn_clicked(self):
text, ok = QInputDialog.getText(self, '文本输入框', '请输入姓名')
if ok and text:
self.name_edit.setText(text)
if __name__ == '__main__':
app = QApplication(sys.argv)
test = QInputDialogDemo()
test.show()
sys.exit(app.exec_())
5 QFontDialog
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys,os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class QFontDialogDemo(QWidget):
def __init__(self):
super(QFontDialogDemo, self).__init__()
self.resize(400,200)
self.setWindowTitle("QFontDialogDemo")
self.layout = QGridLayout(self)
self.layout.addWidget(QLabel("字体: "),0,0,1,1)
self.font_edit = QLineEdit()
self.font_edit.setReadOnly(True)
self.layout.addWidget(self.font_edit, 0, 1, 1, 1)
self.layout.addWidget(QLabel("大小: "),1, 0, 1, 1)
self.font_size = QSpinBox()
self.font_size.setEnabled(False)
self.layout.addWidget(self.font_size, 1, 1, 1, 1)
self.setbtn = QPushButton("选择字体")
self.layout.addWidget(self.setbtn, 2, 0, 1, 2)
self.showtext = QLabel("棋邪纵横子")
self.showtext.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.showtext, 3, 0, 3, 2)
self.setbtn.clicked.connect(self.font_chose_clicked)
def font_chose_clicked(self):
self.font, ok = QFontDialog.getFont()
if ok:
print(self.font)
self.font_edit.setText(self.font.family())
self.font_size.setValue(self.font.pointSize())
self.showtext.setFont(self.font)
if __name__ == '__main__':
app = QApplication(sys.argv)
test = QFontDialogDemo()
test.show()
sys.exit(app.exec_())