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

2024-06-16 10:05:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码有问题。我想在python文件中用kv语言实现一个字符串,为“MDTextFieldClear”添加一个设计。我不确定错误是否一定是在千伏串,但经过一点测试与类和千伏串的压痕,我认为这可能是原因。 下面是一些代码:

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


Tags: textinimportselfhelperinitmodeline
1条回答
网友
1楼 · 发布于 2024-06-16 10:05:34

问题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作为根。在

在主.py在

^{pr2}$

输出

Result

相关问题 更多 >