2015-10-18 67 views
2

我是kivy的新手,並嘗試從kv生成的按鈕中運行myApp類中的do_login函數。Kivy運行kv按鈕的功能

我千伏佈局按鈕

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

我與含有do_login功能

class MyApp(App): 

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

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

類如何使用on_press打電話do_login?

與on_press:do_login(login.text,password.text) '我得到 'do_login' 沒有定義,並與self.do_login同我得到MaterialRaisedButton' 對象有沒有屬性 'do_login'

回答

3

使do_login的MyApp的類的成員:

class MyApp(App): 

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

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

,並使用在kvapp作爲關鍵字來訪問MyApp的並調用該函數:

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