如何解决获取 QMessageBox addButton function
我正在尝试编写一个函数来更好地管理我正在设计的程序的 QMessageBoxes。它接受许多参数并根据这些参数创建一个自定义的 QMessageBox。
def alert(**kwargs):
# Initialization
msg = QMessageBox()
try:
# Conditioning for user selection of QMessageBox Properties
for key,value in kwargs.items():
key = key.lower()
# Set TitleBox value
if key == "title":
msg.setwindowTitle(value)
# Set TextBox value
elif key == "text":
msg.setText(value)
# Set Custom Buttons
elif key == "buttons":
buttons = value.split(',')
for x in range(len(buttons)):
msg.addButton(QPushButton(buttons[x]),QMessageBox.ActionRole)
msg.exec_()
except Exception as error:
print(error)
alert(title="Some Title",text="Some Text",buttons="Yes,No,Restore,Config")
但是,我无法获取按下按钮的值。我尝试了以下解决方案,但没有解决我的问题。
-
msg.buttonClicked.connect(someFunction)
这会将按钮的值传递给一个函数,但我想在我的 alert() 函数中访问单击按钮的值。
解决方法
您必须使用 clickedButton() 方法返回按下的按钮。
import sys
from PyQt5.QtWidgets import QApplication,QMessageBox,QPushButton
def alert(**kwargs):
# Initialization
msg = QMessageBox()
for key,value in kwargs.items():
key = key.lower()
if key == "title":
msg.setWindowTitle(value)
elif key == "text":
msg.setText(value)
elif key == "buttons":
for text in value.split(","):
button = QPushButton(text.strip())
msg.addButton(button,QMessageBox.ActionRole)
msg.exec_()
button = msg.clickedButton()
if button is not None:
return button.text()
if __name__ == "__main__":
app = QApplication(sys.argv)
text = alert(title="Some Title",text="Some Text",buttons="Yes,No,Restore,Config")
print(text)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。