企业网站推广技巧有哪些,江门网站制作维护,十堰论坛网站,澧县住房和城乡建设局网站本文目录 PyQt5桌面应用系列对话框QDialogQDialog的基本用法按钮组 QMessageBox综合展示的例子结论 PyQt5桌面应用系列 PyQt5桌面应用开发#xff08;1#xff09;#xff1a;需求分析 PyQt5桌面应用开发#xff08;2#xff09;#xff1a;事件循环 PyQt5桌面应用开发QDialogQDialog的基本用法按钮组 QMessageBox综合展示的例子结论 PyQt5桌面应用系列 PyQt5桌面应用开发1需求分析 PyQt5桌面应用开发2事件循环 PyQt5桌面应用开发3并行设计 PyQt5桌面应用开发4界面设计 PyQt5桌面应用开发5对话框 [PyQt5桌面应用开发7文本编辑语法高亮与行号](https://withstand.blog.csdn.net/article/details/130486145
对话框
对话框在GUI中是一个常见的组件用于和用户进行交互。这个交互分为两个部分。
为用户提示信息获取用户的注意为用户提供输入信息的界面。
PyQt5提供了一些内置的对话框也可以自定义对话框。所有的PyQt5对话框都是QDialog的子类。
QDialog
QDialog的基本用法
QDialog定义的信号有
accepted()用户点击了对话框的确认按钮finished(int result)用户点击了对话框的确认或取消按钮result是QDialog的返回值QDialog的返回值是QDialog.Accepted或QDialog.Rejected。rejected()用户点击了对话框的取消按钮。
还包括从QWidget继承的信号
updateMicroFocus()更新对话框的焦点。windowTitleChanged(const QString title)对话框的标题发生了变化。customContextMenuRequested(const QPoint pos)用户请求了自定义的上下文菜单。windowIconChanged(const QIcon icon)对话框的图标发生了变化。windowIconTextChanged(const QString iconText)对话框的图标文字发生了变化。windowModalityChanged(Qt::WindowModality windowModality)对话框的模态性发生了变化。windowOpacityChanged(qreal level)对话框的透明度发生了变化。windowStateChanged(Qt::WindowStates oldState, Qt::WindowStates newState)对话框的状态发生了变化。
实际上我们需要关注的就是accepted()和rejected()信号。目前PyQt5推荐采用open()方法显示对话框这种情况下就应该连接finished信号来处理对话框的返回值。这一点应该引起注意好多网上的例子都用exec()等方法来显示对话框这不是PyQt5推荐的方法官方文档中专门针对这个进行了说明。 Note: Avoid using this function; instead, use open(). Unlike exec(), open() is asynchronous, and does not spin an additional event loop. This prevents a series of dangerous bugs from happening (e.g. deleting the dialog’s parent while the dialog is open via exec()). When using open() you can connect to the finished() signal of QDialog to be notified when the dialog is closed. 注意避免使用这个函数而是使用open()。与exec()不同open()是异步的不会再启动一个事件循环。这样可以避免一系列的危险bug例如当对话框通过exec()打开时删除对话框的父窗口。当使用open()时可以连接到QDialog的finished()信号以便在对话框关闭时得到通知。 我们可以自己定义QPushButton然后连接clicked信号到accept()或reject()槽这样就可以在点击按钮时关闭对话框。
按钮组
但是PyQt5还提供了一些默认的按钮组件。
QDialogButtonBox.OkQDialogButtonBox.OpenQDialogButtonBox.SaveQDialogButtonBox.CancelQDialogButtonBox.CloseQDialogButtonBox.DiscardQDialogButtonBox.ApplyQDialogButtonBox.ResetQDialogButtonBox.RestoreDefaultsQDialogButtonBox.HelpQDialogButtonBox.SaveAllQDialogButtonBox.YesQDialogButtonBox.YesToAllQDialogButtonBox.NoQDialogButtonBox.AbortQDialogButtonBox.RetryQDialogButtonBox.IgnoreQDialogButtonBox.NoButton
典型的用法是
QBtn QDialogButtonBox.Ok | QDialogButtonBox.Cancel# connect buttons to slots to set result value of the dialog
self.buttonBox QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)QDialog是一个QWidget所以可以使用通用的布局组件来放置所有的控件并有所有属性操作例如setWindowTitle()方法设置对话框的标题。QDialogButtonBox也按照需求放在布局中。
QMessageBox
为了方便展示信息PyQt5提供了QMessageBox组件。QMessageBox提供了一些静态方法可以直接调用例如
QMessageBox.about()QMessageBox.aboutQt()QMessageBox.critical()QMessageBox.information()QMessageBox.question()QMessageBox.warning()QMessageBox.error()
也可以创建一个QMessageBox实例然后调用open()方法显示对话框。这时可以用message_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)方法设置默认按钮。
这里有很多可以选的按钮例如
QMessageBox.OkQMessageBox.OpenQMessageBox.SaveQMessageBox.CancelQMessageBox.CloseQMessageBox.DiscardQMessageBox.ApplyQMessageBox.ResetQMessageBox.RestoreDefaultsQMessageBox.HelpQMessageBox.SaveAllQMessageBox.YesQMessageBox.YesToAllQMessageBox.NoQMessageBox.NoToAllQMessageBox.AbortQMessageBox.RetryQMessageBox.IgnoreQMessageBox.NoButton
QMessageBox的返回值对应于这些按钮例如QMessageBox.YesQMessageBox.NoQMessageBox.Cancel等。
综合展示的例子
综合上面的例子可以整一个综合的例子包括QDialogQMessageBoxQDialogButtonBox等。
import sysfrom PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QHBoxLayout, QPushButton, QWidget, QVBoxLayout, \QTextEdit, QDialogButtonBox, QLabel, QMessageBoxclass EmptyDialog(QDialog):def __init__(self, parentNone):super(EmptyDialog, self).__init__(parent)self.setWindowTitle(BigEmptyDialog)self.resize(400, 300)class CustomDialog(QDialog):def __init__(self, parentNone):super(CustomDialog, self).__init__(parent)self.setWindowTitle(ButtonBox)QBtn QDialogButtonBox.Ok | QDialogButtonBox.Cancel# connect buttons to slots to set result value of the dialogself.buttonBox QDialogButtonBox(QBtn)self.buttonBox.accepted.connect(self.accept)self.buttonBox.rejected.connect(self.reject)self.layout QVBoxLayout()message QLabel(Something happened, is that OK?)self.layout.addWidget(message)self.layout.addWidget(self.buttonBox)self.setLayout(self.layout)def make_buttons(parent: QWidget, output: QTextEdit):button1 QPushButton(Show EmptyDialog, parent)dlg1 EmptyDialog(parent)button1.clicked.connect(dlg1.open)dlg1.finished.connect(lambda result: output.append(fEmptyDialog finished.))button2 QPushButton(Show CustomDialog, parent)dlg2 CustomDialog(parent)button2.clicked.connect(dlg2.open)dlg2.finished.connect(lambda result: output.append(fCustomDialog result: {resultQDialog.Accepted}))button3 QPushButton(MessageBox, parent)dlg3 QMessageBox(parent)dlg3.setWindowTitle(I have a question!)dlg3.setText(This is a simple dialog)dlg3.setStandardButtons(QMessageBox.Yes | QMessageBox.No)dlg3.setIcon(QMessageBox.Question)dlg3.finished.connect(lambda result: output.append(fMessageBox result: {result QMessageBox.Yes}))button3.clicked.connect(dlg3.open)bs []for builtins, label in zip([QMessageBox.about,QMessageBox.critical,QMessageBox.information,QMessageBox.question,QMessageBox.warning, ], [about, critical, information, question, warning]):button QPushButton(fShow QMessageBox.{label})button.clicked.connect(lambda checked: builtins(parent, fQMessageBox.{label}, fIs {label} look fine?))bs.append(button)bs.extend([button1, button2, button3])return bsif __name__ __main__:app QApplication(sys.argv)win QMainWindow()win.setWindowTitle(Dialog buttons)# center widgetcenter QWidget(win)border QHBoxLayout()center.setLayout(border)text QTextEdit(center)button_layout QVBoxLayout()button_layout.setAlignment(Qt.AlignTop)buttons make_buttons(center, text)for b in buttons:button_layout.addWidget(b)border.addLayout(button_layout)border.setStretch(0, 1)center_layout QHBoxLayout()border.addLayout(center_layout)border.setStretch(1, 6)center_layout.addWidget(text)# Show the windowwin.setCentralWidget(center)win.setMinimumSize(1024, 768)win.setWindowIcon(QIcon(QPixmap(icon.png)))win.show()sys.exit(app.exec_())结论
QDialog是一个QWidget所以可以制作任意复杂的对话框。QMessageBox提供了一个快速的实现。QDialogButtonBox提供了一组标准的按钮可以方便的使用。QDialog及其子类会找其parent搜索Icon。