PyQt5对话框
Posted chusiyong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyQt5对话框相关的知识,希望对你有一定的参考价值。
QMessageBox
功能:一种弹出式的对话框,用于显示消息,或是允许用户进行点击选中
接口:
方法 | 描述 |
information(parent, title, text, buttons, defaultButton) |
弹出信息对话框 parent:指定父窗口 title:对话框标题 text:对话框内容 buttons:多个标准按钮 defaultButton:默认选中按钮 |
question(parent, title, text, buttons, defaultButton) | 弹出问答对话框 |
warning(parent, title, text, buttons, defaultButton) | 弹出警告对话框 |
critical(parent, title, text, buttons, defaultButton) | 弹出严重错误对话框 |
about(parent, title, text) | 唐初关于对话框 |
对话框中的标准按钮:
类型 | 描述 |
QMessageBox.Ok | 同意操作 |
QMessageBox.Cancle | 取消操作 |
QMessageBox.Yes | 同意操作 |
QMessageBox.No | 取消操作 |
QMessageBox.Abort | 终止操作 |
QMessageBox.Retry | 重试操作 |
QMessageBox.Ignore | 忽略操作 |
QinputDialog
功能:有一个文本框和两个按钮组成,提供给用户输入功能
接口:
方法 | 描述 |
getInt() | 从控件中获取整型数据 |
getDouble() | 从控件中获取浮点型数据 |
getText() | 从控件中获取字符串 |
getItem() | 从控件中获取列表里的选择项 |
QFontDialog
功能:字体选择对话框,用户选择字体的大小,样式以及格式
接口:
方法 | 描述 |
getFont | 弹出字体选择对话框 |
QFileDialog
功能:用于打开和保存文件的对话框
接口:
方法 | 描述 |
getOpenFileName() | 弹出文件对话框 |
setFileMode() |
设置可选择的文件类型 QFileDialog.AnyFile 任何文件 QFileDialog.ExistingFile 存在的文件 QFileDialog.Directory 文件夹 QFileDialog.ExistingFiles 已经存在的多个文件 |
setFilter() | 设置过滤器,只显示过滤器允许的文件 |
例子
1.弹出各种对话框
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox class MyWidget(QWidget): def __init__(self): super(MyWidget, self).__init__() infoButton = QPushButton(self) infoButton.setText(‘info‘) infoButton.move(10, 10) infoButton.clicked.connect(self.onInfo) quesButton = QPushButton(self) quesButton.setText(‘ques‘) quesButton.move(10, 50) quesButton.clicked.connect(self.onQues) warnButton = QPushButton(self) warnButton.setText(‘warn‘) warnButton.move(10, 90) warnButton.clicked.connect(self.onWarn) ctcButton = QPushButton(self) ctcButton.setText(‘ctc‘) ctcButton.move(10, 130) ctcButton.clicked.connect(self.onCtc) aboutButton = QPushButton(self) aboutButton.setText(‘about‘) aboutButton.move(10, 170) aboutButton.clicked.connect(self.onAbout) def onInfo(self): reply = QMessageBox.information(self, ‘Information Dialog‘, ‘hello PyQt‘, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: print(‘you choise Yes‘) elif reply == QMessageBox.No: print(‘you choise No‘) pass def onQues(self): reply = QMessageBox.question(self, ‘Question Dialog‘, ‘are you ok?‘, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: print(‘you choise Yes‘) elif reply == QMessageBox.No: print(‘you choise No‘) pass def onWarn(self): reply = QMessageBox.warning(self, ‘Warning Dialog‘, ‘Danger! Deep!‘, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: print(‘you choise Yes‘) elif reply == QMessageBox.No: print(‘you choise No‘) pass def onCtc(self): reply = QMessageBox.critical(self, ‘Critical Dialog‘, ‘Critical Danger!‘, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: print(‘you choise Yes‘) elif reply == QMessageBox.No: print(‘you choise No‘) pass def onAbout(self): QMessageBox.about(self, ‘About Dialog‘, ‘Version: 0.0.0‘) pass if __name__ == ‘__main__‘: app = QApplication(sys.argv) w = MyWidget() w.resize(500, 300) w.move(300, 300) w.setWindowTitle(‘Simple‘) w.show() sys.exit(app.exec_())
2.使用输入对话框获取用户输入的数据
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QInputDialog class MyWidget(QWidget): def __init__(self): super(MyWidget, self).__init__() button1 = QPushButton(self) button1.setText(‘button1‘) button1.move(10, 10) button1.clicked.connect(self.onButton1) button2 = QPushButton(self) button2.setText(‘button2‘) button2.move(10, 50) button2.clicked.connect(self.onButton2) button3 = QPushButton(self) button3.setText(‘button3‘) button3.move(10, 90) button3.clicked.connect(self.onButton3) def onButton1(self): items = (‘C‘, ‘C++‘, ‘Python‘, ‘Go‘) item, ok = QInputDialog.getItem(self, ‘select input dialog‘, ‘语言列表‘, items, 0, False) if ok and item: print(item) def onButton2(self): text, ok = QInputDialog.getText(self, ‘test input dialog‘, ‘输入姓名‘) if ok: print(text) def onButton3(self): num, ok = QInputDialog.getInt(self, ‘test input dialog‘, ‘输入年龄‘) if ok: print(num) if __name__ == ‘__main__‘: app = QApplication(sys.argv) w = MyWidget() w.resize(500, 300) w.move(300, 300) w.setWindowTitle(‘Simple‘) w.show() sys.exit(app.exec_())
3.获取用户选择的字体
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFontDialog class MyWidget(QWidget): def __init__(self): super(MyWidget, self).__init__() button = QPushButton(self) button.setText(‘button‘) button.move(10, 10) button.clicked.connect(self.onButton) def onButton(self): font, ok = QFontDialog.getFont() if ok: print(font.family()) print(font.pointSize()) if __name__ == ‘__main__‘: app = QApplication(sys.argv) w = MyWidget() w.resize(500, 300) w.move(300, 300) w.setWindowTitle(‘Simple‘) w.show() sys.exit(app.exec_())
4.使用文件对话框选择文件
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog class MyWidget(QWidget): def __init__(self): super(MyWidget, self).__init__() button = QPushButton(self) button.setText(‘button‘) button.move(10, 10) button.clicked.connect(self.onButton) button2 = QPushButton(self) button2.setText(‘button2‘) button2.move(10, 50) button2.clicked.connect(self.onButton2) def onButton(self): fname, ftype = QFileDialog.getOpenFileName(self, ‘Open File‘, ‘C:\‘) print(fname) print(ftype) def onButton2(self): dlg = QFileDialog() dlg.setFileMode(QFileDialog.ExistingFiles) if dlg.exec_(): fname = dlg.selectedFiles() for f in fname: print(f) if __name__ == ‘__main__‘: app = QApplication(sys.argv) w = MyWidget() w.resize(500, 300) w.move(300, 300) w.setWindowTitle(‘Simple‘) w.show() sys.exit(app.exec_())
以上是关于PyQt5对话框的主要内容,如果未能解决你的问题,请参考以下文章