2
PyQt5系列教程(二十四)QMessageBox中的按钮自定义
source link: https://xugaoxiang.com/2022/04/10/pyqt5-24-qmessagebox-button-customization/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
软硬件环境
- Windows 10 64bit
- Anaconda3 with python 3.7
- PyQt5
QMessageBox
是一个通用的弹出对话框,包括了提示、警告、错误、信息、关于等等,只是显示的图标不一样,基本功能是非常类似的,本文来看看如何在 QMessageBox
中自定义按钮。
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox, QPushButton
class QMessageBoxDemo(QMainWindow):
def __init__(self, parent=None):
super(QMessageBoxDemo, self).__init__(parent)
self.setWindowTitle("QMessageBox demo")
self.resize(600, 400)
self.button = QPushButton(self)
self.button.setText("点击弹出QMessageBox")
self.button.resize(180, 140)
self.button.move(200, 100)
self.button.clicked.connect(self.showDialog)
def showDialog(self):
msgBox = QMessageBox(QMessageBox.Warning, "标题", "消息正文")
yes = msgBox.addButton("自定义Yes按钮", QMessageBox.YesRole)
no = msgBox.addButton("自定义No按钮", QMessageBox.NoRole)
msgBox.exec_()
if msgBox.clickedButton() == yes:
print('yes')
else:
print('no')
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = QMessageBoxDemo()
demo.show()
sys.exit(app.exec_())
https://github.com/xugaoxiang/learningPyQt5
PyQt5系列教程
更多PyQt5
教程,请移步
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK