简体   繁体   中英

PyQt: translating standard buttons

How can I easily translate standard buttons (Yes, No) from QMessageBox? I can't use self.tr on those arguments, so I would like to achieve it in some other simple way. Do I have to use whole translation system?

Here is how I did :

First you need to copy the qt_LOCALE.qm file to your application directory. Mine was :

cp /usr/share/qt4/translations/qt_fr.qm .

Secondly, you need to load a translator for your application.

application = QApplication(argv)

locale = QLocale.system().name()
qtTranslator = QTranslator()
if qtTranslator.load("qt_"+locale):
    application.installTranslator(qtTranslator)

main_window = QMainWindow()
main_window.show()

exit(application.exec_())

Are you sure you have to translate them yourself? In other toolkits, standard buttons are automatically translated to the language that the user is using.

The quickest way is something like this:

mymessagebox.button(mymessagebox.Yes).setText("Yes, please!")
mymessagebox.button(mymessagebox.No).setText("No, thanks.")

See the QMessageBox documentation for all possible standard buttons .

If you want to have Yes/No translated into the current language, usually setting the system language should be enough. Otherwise try with Natim's suggestion.

If you want custom texts: The static methods like question() etc. don't allow custom button texts. That's unfortunate, as "Yes"/"No" dialogs are considered bad style (they are not descriptive, one has to read the whole message, and its easy to misunderstand what means Yes and what means No and accidentally negating them). For custom texts, you have to use QMessageBox directly. I'd suggest to write your own static methods accepting optional button texts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM