首页 > 解决方案 > Python kivy:如何修复“TypeError:object.__init__() 不带参数”?

问题描述

我的代码有问题。我想在我的python文件中实现一个带有kv语言数据的字符串,以将设计添加到“MDTextFieldClear”。我不确定错误是否必须在 kv 字符串中,但是在对类和 kv 字符串的缩进进行了一些测试之后,我认为这可能是原因。这是一段代码:

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()

错误如下:

回溯(最后一次调用): KivyGUI().run() 中的文件“PATH_TO_MY_PYTHON_FILE”,第 106 行

文件“C:\Users\username\Anaconda3\lib\site-packages\kivy\app.py”,第 800 行,运行 root = self.build()

文件“PATH_TO_MY_PYTHON_FILE”,第 100 行,在构建 c = LayoutPy()

文件“PATH_TO_MY_PYTHON_FILE”,第 54 行,在init self.get_voc = MDTextFieldClear(helper_text="请输入翻译", helper_text_mode="on_focus", max_text_length=12, multiline=False, color_mode="accent")

文件“C:\Users\username\Anaconda3\lib\site-packages\kivy\uix\boxlayout.py”,第 131 行,在init super(BoxLayout, self) 中。初始化(**kwargs)

文件“C:\Users\username\Anaconda3\lib\site-packages\kivy\uix\layout.py”,第 76 行,在init super(Layout, self) 中。初始化(**kwargs)

文件“C:\Users\username\Anaconda3\lib\site-packages\kivy\uix\widget.py”,第 340 行,在init super(Widget, self) 中。初始化(**kwargs)

kivy._event.EventDispatcher 中的文件“kivy_event.pyx”,第 243 行。初始化 类型错误:对象。init () 不带参数

标签: pythonkivykivy-language

解决方案


问题 1 - 类型错误

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

根本原因

错误是由于属性color_mode和/或multiline.

问题 2 - 继承不匹配

在您的 kv 文件中,该属性orientation被声明为类规则,<LayoutPy>:. 此属性适用于BoxLayout。但是在您的 Python 脚本class LayoutPy()中,继承了FloatLayout.

解决方案

以下示例BoxLayout用作根。

主文件

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()

输出

结果


推荐阅读