Python kivy: How can I fix "TypeError: object.__init__() takes no parameters"?
Python kivy: How can I fix "TypeError: object.__init__() takes no parameters"?
我的代码有问题。我想在我的 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 (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
问题 1 - 类型错误
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
根本原因
错误是由于属性 color_mode
and/or multiline
.
问题 2 - 继承不匹配
在您的 kv 文件中,为 class 规则 <LayoutPy>:
声明了属性 orientation
。此属性适用于 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()
输出
我的代码有问题。我想在我的 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 (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
问题 1 - 类型错误
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
根本原因
错误是由于属性 color_mode
and/or multiline
.
问题 2 - 继承不匹配
在您的 kv 文件中,为 class 规则 <LayoutPy>:
声明了属性 orientation
。此属性适用于 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()