I am writing a wrapper for all widgets that contain text that needs to be translated for wxPython and make them handle the initial translation and the change of language on their own using gettext.
I already have a working prototype but I'm sure this is not the correct way to do it. Here is an example for all subclasses of wx.Control
.
My Idea is to override the relevant methods of wx.Control
and then merge the actual widgets (subclasses of wx.Control
) together with my Control
.
import wx
from wx.lib.pubsub import pub
class Control(wx.Control):
def __init__(self, label):
self.ml_label = label
pub.subscribe(self.Update, 'language.changed')
def Update(self):
super().Update()
super().SetLabel(_(self.ml_label))
def SetLabel(self, label):
self.ml_label = label
super().SetLabel(_(self.ml_label))
class Button(wx.Button, Control):
def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
validator=wx.DefaultValidator, name=wx.ButtonNameStr):
Control.__init__(self, label)
wx.Button.__init__(self, parent, id, _(label), pos, size, style, validator, name)
class StaticText(wx.StaticText, Control):
def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
name=wx.StaticTextNameStr):
Control.__init__(self, label)
wx.StaticText.__init__(self, parent, id, _(label), pos, size, style, name)
Before I had the code from the Control
class in Button
and StaticText
but as there are lots of widgets based on Control
I wanted to move the duplicate code to one place.
It works, but I'm sure it's not the right way to do it especially as I don't call super().__init__
in the Control
class, which can't be right.
Should I even be using multiple inheritance for what I am trying to do here?