【问题标题】:Kivy run function from kv button来自 kv 按钮的 Kivy 运行功能
【发布时间】:2016-01-16 18:19:49
【问题描述】:

我是 kivy 的新手,我尝试通过 kv 生成的按钮在 MyApp 类中运行 do_login 函数。

我的带有按钮的 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'

【问题讨论】:

    标签: python function class kivy


    【解决方案1】:

    使 do_login 成为 MyApp 类的成员:

    class MyApp(App):
    
        def build(self):
            main_widget = Builder.load_string(login_kv) 
    
        def do_login(self, *args):
            print'jo'
    

    并使用kv中的app作为关键字访问MyApp并调用函数:

    on_press: app.do_login()
    

    来自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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多