kivy __init__需要1个后处理论据但是给出了2个

时间:2017-06-20 16:09:00

标签: python kivy

所以我正在学习Kivy的学校项目,我在测试按钮时遇到了错误。这是我的代码:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
from kivy.uix.button import Button


class übung(GridLayout):

    def lol(instance):
         label.disabled = False


    def __init__(self):
        super(übung, self).__init__
        self.cols = 2
        self.label = Label ("Ehrm ... lol")
        label.disabled = True
        self.btn1 = Button(text="Hello world 1")
        self.btn1.bind(on_press=lol)
        self.btn2 = Button(text="Hello world 2")
        self.btn2.bind(on_press=lol)

class App(App):
     def build(self):
        return übung()


if __name__ == "__main__":
    App().run()

我得到的错误在标题中( init 需要1个postitional参数但是给出了2个)。它应该是两个按钮,如果你按一个它说ehrm ...大声笑。正如我所说,它仅用于测试目的。

先谢谢, 我

1 个答案:

答案 0 :(得分:1)

您有几个错误。您显示的错误是因为您必须按名称将参数(text)传递给Label构造函数:

self.label = Label (text="Ehrm ... lol")

您的代码应如下所示:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button


class übung(GridLayout):
    def __init__(self, **kwargs):
        super(übung, self).__init__(**kwargs)
        self.cols = 2
        self.label = Label(text = "Ehrm ... lol")
        self.label.disabled = True
        self.btn1 = Button(text="Hello world 1")
        self.btn1.bind(on_press=self.lol)
        self.btn2 = Button(text="Hello world 2")
        self.btn2.bind(on_press=self.lol)

        self.add_widget(self.label)
        self.add_widget(self.btn1)
        self.add_widget(self.btn2)

    def lol(self,  event):
        self.label.disabled = False

class App(App):
     def build(self):
        return übung()


if __name__ == "__main__":
    App().run()