【问题标题】:Kivy .kv file use function defined in outside file外部文件中定义的 Kivy .kv 文件使用函数
【发布时间】:2018-10-06 21:39:48
【问题描述】:

3 个文件。 test.py, myfunctions.py & test.kv

试图从 test.kv 中的 myfunctions.py 调用一个函数

test.py

from kivy.app import App
import myfunctions

class TestApp(App):
    title = 'Test Application'
    pass

if __name__ == '__main__':
    TestApp().run()

myfunctions.py

def myfunc():
    print('hello world')

test.kv

GridLayout:
    Button:
        text: "My Button"
        on_release: myfunc()

我也尝试过在应用程序类中定义函数(根据 kivy 文档) - 没用

我错过了什么?

【问题讨论】:

    标签: python-3.x kivy


    【解决方案1】:

    正如the docs 指出的,你必须在.kv 中导入函数:

    #:import myfunc myfunctions.myfunc
    
    GridLayout:
        Button:
            text: "My Button"
            on_release: myfunc()
    

    【讨论】:

    • 我明白了,我做到了... #:import f1 myfunctions
    • @ZacharySutton 所以如果我的回答对你有帮助,别忘了把它标记为正确,如果你不知道怎么做,请查看tour,这是最好的感谢方式。跨度>
    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多