从 QMessageBox 标准按钮获取系统默认标签



我想在QMessageBox的信息文本中显示"是"和"否"按钮的文本,但我不明白如何从按钮中获取这些标签。

from PyQt5.QtWidgets import *
import sys
app = QApplication(sys.argv)
msgbox = QMessageBox()
msgbox.setStandardButtons(msgbox.Yes | msgbox.No)
info_text = "Click '{yes}' to confirm. Click '{no}' to abort."
msgbox.setInformativeText(info_text)
if msgbox.exec_() == msgbox.Yes:
    print("Confirmed")
else:
    print("Aborted")

通过调用 setStandardButtons ,按钮顺序和按钮标签将设置为当前操作系统和当前语言设置的默认值。如何获取这些默认值,以便将它们用于字符串info_text中的插槽?

我想过使用 QMessageBox 对象中的 buttons 属性,这是一个QPushButton对象列表。我可以从那里读取标签,但我看不出如何确定列表中的第一个元素是Yes还是No按钮。

好吧

,我很愚蠢:除了buttons属性之外,还有button()方法,它将我要检索的按钮类型作为其参数。然后,我可以使用text()来获取标签。最后,必须从标签中删除热键标记&

info_text = "Click '{yes}' to confirm. Click '{no}' to abort.".format(
    yes=msgbox.button(msgbox.Yes).text().replace("&", ""), 
    no=msgbox.button(msgbox.No).text().replace("&", ""))
msgbox.setInformativeText(info_text)

最新更新