Python kivy:如何修复“TypeError:object.__init__() 不带参数”?

Posted

技术标签:

【中文标题】Python kivy:如何修复“TypeError:object.__init__() 不带参数”?【英文标题】:Python kivy: How can I fix "TypeError: object.__init__() takes no parameters"? 【发布时间】:2019-10-17 14:02:27 【问题描述】:

我的代码有问题。我想在我的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()

错误如下:

Traceback(最近一次调用最后一次): 文件“PATH_TO_MY_PYTHON_FILE”,第 106 行,在 KivyGUI().run()

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

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

init 中的文件“PATH_TO_MY_PYTHON_FILE”,第 54 行 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).init(**kwargs)

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

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

文件“kivy_event.pyx”,第 243 行,在 kivy._event.EventDispatcher.init TypeError: object.init() 没有参数

【问题讨论】:

是的,你应该指向错误来自的行和消息。到目前为止,我只能猜测这里的 super(LayoutPy, self).__init__(**kwargs) 这个 LayoutPy。 init 不需要任何参数 我现在添加了完整的错误信息 是的,正如我所说的 super(something, self).init(**kwargs) 调用 something.init(**kwargs) 但这个初始化不需要 args,可能是 super(Widget, self).init (**kwargs) 导致跟踪到此结束 请参阅 rhettinger.wordpress.com/2011/05/26/super-considered-super 以获取有关从 __init__ 正确使用 super 的建议。简而言之,像LayoutPy 这样的类必须从kwargs 中删除FloatLayout 不会期望的任何参数,因为否则FloatLayout 会盲目地将它们传递给object,这不会期望任何关键字参数。 你有适合我的代码示例的解决方案吗? 【参考方案1】:

问题 1 - 类型错误

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

根本原因

错误是由color_mode 和/或multiline 属性引起的。

问题 2 - 继承不匹配

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

解决方案

以下示例使用BoxLayout 作为根。

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

输出

【讨论】:

非常感谢您的帮助。最后它的工作。我试图在 kv 字符串中设置 BoxLayout,这样小部件就不会一直显示在底部。看起来不错:D

以上是关于Python kivy:如何修复“TypeError:object.__init__() 不带参数”?的主要内容,如果未能解决你的问题,请参考以下文章

如何修复 ubuntu 中的“No module named 'kivy._clock'”错误?

使用 buildozer 打包我的 kivy 应用程序时如何修复“java.lang.module.FindException:找不到模块 java.se.ee”错误

找不到 TableRecycleGridLayout 来修复 Kivy 代码

Python+kivy+SQLite:如何一起使用

Python:如何在 Kivy 中创建一个简单的框架?

如何在 python 中获取用户输入(Kivy 框架)