Python / Kivy中的__init__参数

时间:2019-08-11 01:15:20

标签: python kivy

我正在尝试调用 init 方法,它是函数的父级。我可能正在做一些显然很愚蠢的事情,但是我无法弄清楚。当我收到以下代码时,我得到一个错误:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty, ListProperty
from kivy.clock import Clock
from kivy.graphics import Color

class FieldCommand(Widget):
    def __init__(self, **kwargs):
        super(FieldCommand, self).__init__(**kwargs)
        self.myBoard = Board(col=2, rows=2)
        for c in range(4):
            self.add_widget(Button(text = c))

class Board(Widget):
    def __init__(self, **kwargs):
        super(Board, self).__init__(**kwargs)



class FieldCommandApp(App):
    def build(self):
        return FieldCommand()


if __name__ == '__main__':
    FieldCommandApp().run()

具体地说,我的错误是:

File "/Users/.../PycharmProjects/FieldCommand/venv/lib/python3.7/site-packages/kivy/uix/widget.py", line 350, in __init__
     super(Widget, self).__init__(**kwargs)
   File "kivy/_event.pyx", line 243, in kivy._event.EventDispatcher.__init__
 TypeError: object.__init__() takes exactly one argument (the instance to initialize

) 我在跟踪示例here

class Container(BoxLayout):


    def __init__(self, **kwargs):
        super(Container, self).__init__(**kwargs)
        self.previous_text = open(self.kv_file).read()
        parser = Parser(content=self.previous_text)
        widget = Factory.get(parser.root.name)()
        Builder._apply_rule(widget, parser.root, parser.root)
        self.add_widget(widget)

为什么我的应用程序出现错误?

1 个答案:

答案 0 :(得分:1)

您对self.myBoard = Board(col=2, rows=2)的呼叫包括kwargscol的{​​{1}},这些呼叫已传递到rows Widget,但不接受矮人因此,解决方法是提取__init__()类中的kwargs。您可以这样简单地在Board类中添加Properties

Board