gpt4 book ai didi

python - 如何更改QMessageBox中的按钮顺序

转载 作者:行者123 更新时间:2023-12-01 03:57:23 33 4
gpt4 key购买 nike

该代码创建一个带有单个QPushButtonQDialog窗口。单击该按钮将打开带有三个按钮的QMessageBox 窗口。有没有办法重新排列按钮顺序?

enter image description here

from PyQt4 import QtGui, QtCore
app = QtGui.QApplication([])

class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)

self.resize(300, 100)
self.setLayout(QtGui.QVBoxLayout())

button = QtGui.QPushButton('Submit')
button.clicked.connect(self.onclick)
self.layout().addWidget(button)

def onclick(self):
self.close()
messagebox = QtGui.QMessageBox(QtGui.QMessageBox.Warning, "Title text", "body text", buttons = QtGui.QMessageBox.Ok | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel, parent=self)
messagebox.setDefaultButton(QtGui.QMessageBox.No)
exe = messagebox.exec_()
print exe

dialog = Dialog()
dialog.show()
app.exec_()

最佳答案

QMessageBox.addButton() 重载可用于按钮排序。默认顺序因平台而异。调用exec()后使用QMessageBox.clickedButton()查询使用了什么按钮。

enter image description here

from PyQt4 import QtGui, QtCore
import sys
app = QtGui.QApplication([])

class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)

self.resize(300, 100)
self.setLayout(QtGui.QVBoxLayout())

button = QtGui.QPushButton('Submit')
button.clicked.connect(self.onclick)
self.layout().addWidget(button)

def onclick(self):
self.close()
message = "<font size = 5 color = gray > Rich Html Title </font> <br/><br/>The clickable link <a href='http://www.google.com'>Google.</a> The lower and upper case text."
messagebox = QtGui.QMessageBox(QtGui.QMessageBox.Warning, "title", message, parent=self)
messagebox.addButton("ResetRole Left Most", QtGui.QMessageBox.ResetRole)
messagebox.addButton("ApplyRole Left", QtGui.QMessageBox.ApplyRole)
messagebox.addButton("RejectRole Right", QtGui.QMessageBox.RejectRole)
messagebox.addButton("NoRole Right Most", QtGui.QMessageBox.NoRole)
exe = messagebox.exec_()
print 'exe: %s clickedButton: %s'%(exe, messagebox.clickedButton())

dialog = Dialog()
dialog.show()
app.exec_()

关于python - 如何更改QMessageBox中的按钮顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37219153/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com