Tkinter MessageBox text on custom Button

Viewed 122

On windows, PredefinedType of Tkinter.Messagebox (for exemple retrycancel) are shown in the Windows default language (retry/cancel if en_US, Recommencer/Annuler if fr_FR, and so on).

Is there a way to retrieve those Windows label in order to correctly initialize a custom Button with the respective language translation.

Tkinter.Button(text=????)

1 Answers

It is possible to retrieve the translated strings through the tcl interpreter:

import tkinter as tk

def translate(text):
    return root.tk.eval("namespace eval ::tk {::msgcat::mc %s}" % text)


root = tk.Tk()
tk.Button(root, text=translate("retry")).pack()
root.mainloop()

Tcl uses the package msgcat for the internationalization so ::msgcat::mc string returns the translation for string in the current locale. The namespace eval is needed to access the translations loaded for the tk package.

Related