python - 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 () 不带参数
解决方案
问题 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()
输出
推荐阅读
- julia - 我可以像在 Fortran 中一样在 Julia 中定义变量吗
- reactjs - 从注册在 useEffect 的回调中获取最新状态
- c# - r闪亮的ggplot在使用R.net的WPF C#中不起作用
- python - 尝试为警报消息实现两个 python 文件
- python - TypeError: __init_subclass__() 不接受与子类和抽象类设计相关的关键字参数
- python - 如何翻译模型标题?
- php - PHP Foreach 只获取一个数据
- java - 在 Android Studio 中使用 ListView 时出现运行时错误(空指针异常)
- vba - MSAccess:使用 VBAccess 隐藏导航窗格和菜单栏
- visual-studio-code - 是否有一个 vscode 插件能够在两列中顺序显示相同的文件?