我这里的代码有问题。我想在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()
错误如下:
回溯(最近一次调用last):KivyGUI()中第106行的文件“PATH_TO_MY_PYTHON_File”。运行()
文件“C:\Users\username\Anaconda3\lib\site packages\kivy\app.py”,第800行,在run root=self中。构建()
文件“路径到我的PYTHON文件”,第100行,在build c=LayoutPy()中
文件“路径到我的PYTHON文件”,第54行,在init self中。get_voc=MDTextFieldClear(helper_text=“请输入翻译”,helper_text_mode=“on_focus”,max_text_length=12,multiline=False,color_mode=“accent”)
文件"C:\用户\用户名\Anaconda3\lib\site-包\kivy\uix\boxlayout.py",第131行,在init超级(BoxLayout,自我). init(**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.pyx”,第243行,基维语。_event。EventDispatcher.initTypeError:object.init()不带参数
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
错误是由于属性、color\u mode
和/或多行
造成的。
在kv文件中,为类规则
以下示例使用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()