5
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

After I posted this question and saw that I wrote that I try to write a Wrapper, I thought that this might be the solution...

import wx
from wx.lib.pubsub import pub

def multilingual(wrapped):

    class WrappedClass(wrapped):
        def __init__(self, *args, **kwargs):
            # Translate label parameter
            if len(args) >= 3:
                self.ml_label = args[2]
                args = list(args)
                args[2] = _(self.ml_label)
            else:
                self.ml_label = kwargs.get('label',wx.EmptyString)
                kwargs['label'] = _(self.ml_label)
            super().__init__(*args,**kwargs)
            pub.subscribe(self.Update, 'language.changed')

        def Update(self):
            print('update')
            super().Update()
            super().SetLabel(_(self.ml_label))

        def SetLabel(self, label):
            print('setlabel')
            self.ml_label = label
            super().SetLabel(_(self.ml_label))

    return WrappedClass

@multilingual
class Button(wx.Button):
    pass

@multilingual
class StaticText(wx.StaticText):
    pass
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.