简体   繁体   中英

Kivy run function from kv button

I'm new at kivy and try to run a do_login function inside my MyApp Class from a kv generated button.

my kv layout with the button

RelativeLayout:
    FloatingActionButton:
        id:                 float_act_btn
        on_press:           ???how to call call do_login from MyApp

and my class with the containing do_login function

class MyApp(App):

    def build(self):
        main_widget = Builder.load_string(login_kv) 

def do_login(self, *args):
    print'jo'

How to use on_press to call do_login?

with on_press:do_login(login.text, password.text)' I get 'do_login' is not defined and the same with self.do_login I get MaterialRaisedButton' object has no attribute 'do_login'

make do_login a member of the MyApp class:

class MyApp(App):

    def build(self):
        main_widget = Builder.load_string(login_kv) 

    def do_login(self, *args):
        print'jo'

and use app in kv as the keyword to access MyApp and call the function:

on_press: app.do_login()

from Kivy language :

There are three keywords specific to Kv language:

app: always refers to the instance of your application.
root: refers to the base widget/template in the current rule
self: always refer to the current widget

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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