I have a multilingual Tkinter application. Since Tkinter itself doesn't expose the underlying msgcat
functionality of Tk, I am using ttkbootstrap
. As of now, I have the translations working, and I destroy and rebuild all the widgets when the language is changed via the interface.
Virtual events are a powerful feature of Tkinter, which has allowed me to structure my application in an OO way while using a combination of control variables and virtual events for inter-widget communication / message passing.
I tried to implement a translation-aware widget by this code, but as I now know, virtual events need widget focus, which won't be the case:
from ttkbootstrap import ttk
from ttkbootstrap.localization import MessageCatalog
def _tr(t: str) -> str:
return MessageCatalog.translate(t)
class TrButton(ttk.Button):
def __init__(self, master=None, text: str = "", **kwargs):
super().__init__(master=master, text=_tr(text), **kwargs)
self.bind(
"<<LanguageChanged>>", lambda *_: self.configure(text=_tr(self["text"]))
)
Now, rebuilding all the widgets is not a very expensive task really for my app, but I was wondering if there was a better way? A way to have truly translation aware widgets?