0

Based on an answer from my previous question, I am trying to inherit the tkinter (ttkbootstrap to be precise) classes in this manner:

class _TrWidget:
    def __init__(
        self, type_: Type[ttk.Widget], master: ttk.Widget, text: str, **kwargs
    ):
        type_(master, text=_tr(text), **kwargs)


class _TrButton(_TrWidget, ttk.Button):
    def __init__(self, master: ttk.Widget = None, text: str = "", **kwargs):
        _TrWidget(ttk.Button, master, text, **kwargs)

gives me the error:

AttributeError: '_TrButton' object has no attribute 'tk'

when I do:

button = _TrButton(master, "Button").pack()

Now this might be completely unrelated to tkinter itself, since I haven't used this particular type of inheritance anywhere else.

demberto
  • 489
  • 5
  • 15
  • 1
    Your two constructors never call parent constructors. They allocate random objects and discard them immediately. Go re-read that answer you linked; it'll show you how to use `super` in Python. – Silvio Mayolo Apr 08 '22 at 15:27
  • Why are you using this type of inheritance rather than the method shown in the other answer? – Bryan Oakley Apr 08 '22 at 16:40
  • @BryanOakley You told me how to implement it for a button, but I want to use it for all types of widgets – demberto Apr 08 '22 at 17:30
  • I showed how to do it for a button because that's what you used for your question. You can inherit from any widget you want. – Bryan Oakley Apr 08 '22 at 17:52

1 Answers1

1
class _TrWidget(ttk.Widget):
    def __init__(self, master: ttk.Widget, text: str, **kwargs):
        super().__init__(master, text=_tr(text), **kwargs)


class _TrButton(_TrWidget, ttk.Button):
    def __init__(self, master: ttk.Widget = None, text: str = "", **kwargs):
        super().__init__(master, text, **kwargs)

I don't even know how that worked but it did

demberto
  • 489
  • 5
  • 15