简体   繁体   English

单击 kivy 按钮时如何调用 function?

[英]How to call a function when clicked a kivy button?

I'm pretty new to kivy.我对 kivy 很陌生。 I want to call a function in the App class.我想在应用程序 class 中调用 function。 But it gives an error.但它给出了一个错误。 The error is "on_press: app.hello() TypeError: hello() takes 0 positional arguments but 1 was given"错误是“on_press: app.hello() TypeError: hello() 取 0 位置 arguments 但给出了 1”

.kv file code .kv 文件代码

            Button:
            id: add_income_btn
            size_hint: (.05, .2)
            pos: (393, 302)
            background_color: (1, 1, 1, 0)
            text: "+"
            font_size:'20sp'
            on_press: app.hello()

.py file code .py 文件代码

Builder.load_file("test1.kv")
class Money_Manager(App,TabbedPanel): 
    def hello():
        print("Hello")

The methods of an instantiated Class in Python need a self argument (the current Class instance). Python 中实例化的 Class 的方法需要一个self参数(当前 Class 实例)。
So you must use:所以你必须使用:

    def hello(self):
        print("Hello")

Try This One:试试这个:

on_press: app.hello

instead of代替

on_press: app.hello()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM