4

I have a problem with my code here. I want to implement a string with data in the kv language right in my python file to add a design to the "MDTextFieldClear". I am not sure if the error has to be in kv string but after a bit of testing with the classes and the indentation of the kv string I think it could be the reason. Here's a bit of code:

from kivymd.theming import ThemeManager
from kivymd.textfields import MDTextFieldClear    # KivyMD imports

class LayoutPy(FloatLayout):    # Widget class
    def __init__(self, **kwargs):
        super(LayoutPy, self).__init__(**kwargs)
        self.get_voc = MDTextFieldClear(helper_text="Please enter the translation", helper_text_mode="on_focus", max_text_length=12, multiline=False, color_mode="accent")
        self.add_widget(self.get_voc)

        # ... (few more widgets) ...#

Builder.load_string("""
#:import MDTextField kivymd.textfields.MDTextField
#:import MDTextFieldRound kivymd.textfields.MDTextFieldRound
#:import MDTextFieldClear kivymd.textfields.MDTextFieldClear
#:import MDTextFieldRect kivymd.textfields.MDTextFieldRect

<LayoutPy>:
    orientation: 'vertical'
    FloatLayout:
        MDTextFieldClear:
            hint_text: ""
            helper_text: "Enter translation"
            helper_text_mode: "on_focus"
            max_text_length: 10
""")

class KivyGUI(App):          # Main class for build
    theme_cls = ThemeManager()
    theme_cls.primary_palette = ("Blue")
    title = ('Lingu Trainer')
    main_widget = None

    def build(self):
        c = LayoutPy()
        d = Factory.TextFields()
        return c


if __name__ == "__main__":
    KivyGUI().run()

The error is as follows:

Traceback (most recent call last): File "PATH_TO_MY_PYTHON_FILE", line 106, in KivyGUI().run()

File "C:\Users\username\Anaconda3\lib\site-packages\kivy\app.py", line 800, in run root = self.build()

File "PATH_TO_MY_PYTHON_FILE", line 100, in build c = LayoutPy()

File "PATH_TO_MY_PYTHON_FILE", line 54, in init self.get_voc = MDTextFieldClear(helper_text="Please enter the translation", helper_text_mode="on_focus", max_text_length=12, multiline=False, color_mode="accent")

File "C:\Users\username\Anaconda3\lib\site-packages\kivy\uix\boxlayout.py", line 131, in init super(BoxLayout, self).init(**kwargs)

File "C:\Users\username\Anaconda3\lib\site-packages\kivy\uix\layout.py", line 76, in init super(Layout, self).init(**kwargs)

File "C:\Users\username\Anaconda3\lib\site-packages\kivy\uix\widget.py", line 340, in init super(Widget, self).init(**kwargs)

File "kivy_event.pyx", line 243, in kivy._event.EventDispatcher.init TypeError: object.init() takes no parameters

Frenggie
  • 149
  • 4
  • 16
  • yes , you shoud point to the line where the error comes from and msg. so far i can only guess that here super(LayoutPy, self).__init__(**kwargs) this LayoutPy. init takes no args – user8426627 Jun 01 '19 at 01:51
  • I added the full error message now – Frenggie Jun 01 '19 at 02:01
  • yes as i sayd super(something, self).init(**kwargs) calls something.init(**kwargs) but this init takes no args, probably super(Widget, self).init(**kwargs) cause trace ends there – user8426627 Jun 01 '19 at 02:09
  • See https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ for advice on using `super` properly from `__init__`. In short, a class like `LayoutPy` has to remove any argument from `kwargs` that `FloatLayout` won't expect, because otherwise `FloatLayout` will blindly pass them on to `object`, which doesn't expect *any* keyword arguments. – chepner Jun 01 '19 at 13:50
  • Do you have a suitable solution for my code example? – Frenggie Jun 01 '19 at 15:53

1 Answers1

3

Problem 1 - TypeError

 TypeError: object.__init__() takes exactly one argument (the instance to initialize)

Root Cause

The error was due to the attributes, color_mode and/or multiline.

Problem 2 - Inheritance Mismatch

In your kv file, the attribute, orientation is declared for class rule, <LayoutPy>:. This attribute is applicable to BoxLayout. But in your Python script, class LayoutPy() has an inheritance of FloatLayout.

Solution

The following example use BoxLayout as the root.

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

from kivymd.theming import ThemeManager
from kivymd.textfields import MDTextFieldClear


class LayoutPy(BoxLayout):

    def __init__(self, **kwargs):
        super(LayoutPy, self).__init__(**kwargs)

        self.get_voc = MDTextFieldClear(helper_text="Please enter the translation",
                                        helper_text_mode="on_focus", max_text_length=12,
                                        hint_text="Created in py"
                                        )
        self.add_widget(self.get_voc)


Builder.load_string("""
#:import MDTextFieldClear kivymd.textfields.MDTextFieldClear

<LayoutPy>:
    orientation: 'vertical'

    FloatLayout:
        MDTextFieldClear:
            hint_text: "kv: Created"
            helper_text: "Enter translation"
            helper_text_mode: "on_focus"
            max_text_length: 10

""")


class KivyGUI(App):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = "Blue"
    title = 'Lingu Trainer'

    def build(self):
        return LayoutPy()


if __name__ == "__main__":
    KivyGUI().run()

Output

Result

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Many thanks for your help. Finally it's working. I tried to set the BoxLayout in the kv string so the widgets don't show up at the bottom all the time. Looking good :D – Frenggie Jun 01 '19 at 16:10