1. 程式人生 > >PyQT中讓QMessageBox按鈕顯示中文

PyQT中讓QMessageBox按鈕顯示中文

QMessageBox.warning(self,'錯誤', '使用者名稱和密碼不匹配', QMessageBox.Yes, QMessageBox.Yes)

以上一條語句輸出,但更改前這樣顯示的,中英文混合,看著彆扭。

參考了http://srinikom.github.io/pyside-docs/PySide/QtGui/QMessageBox.html上的文件

            self.box = QMessageBox(QMessageBox.Warning, "錯誤", "使用者名稱和密碼不匹配!")
            self.box.addButton(self.tr("確定"), QMessageBox.YesRole)
            #self.box.addButton(self.tr("取消"), QMessageBox.NoRole)
            self.box.show()
self.box = QMessageBox(QMessageBox.Warning, "錯誤", "使用者名稱和密碼不匹配!")
qyes=self.box.addButton(self.tr("確定"), QMessageBox.YesRole)
qno=self.box.addButton(self.tr("取消"), QMessageBox.NoRole)
self.box.exec_()
if self.box.clickedButton() == qyes:
    print('okokok')
else:
    return

其中,引數QMessageBox.YesRole可以為以下幾種:


QMessageBox.InvalidRole The button is invalid.
QMessageBox.AcceptRole Clicking the button causes the dialog to be accepted (e.g. OK).
QMessageBox.RejectRole Clicking the button causes the dialog to be rejected (e.g. Cancel).
QMessageBox.DestructiveRole Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog.
QMessageBox.ActionRole Clicking the button causes changes to the elements within the dialog.
QMessageBox.HelpRole The button can be clicked to request help.
QMessageBox.YesRole The button is a “Yes”-like button.
QMessageBox.NoRole The button is a “No”-like button.
QMessageBox.ApplyRole The button applies current changes.
QMessageBox.ResetRole The button resets the dialog’s fields to default values.