我对使用 kivy 库还很陌生。
我有一个 app.py 文件和一个 app.kv 文件,我的问题是我无法在按下按钮时调用函数。
应用程序.py:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class Launch(BoxLayout):
def __init__(self, **kwargs):
super(Launch, self).__init__(**kwargs)
def say_hello(self):
print "hello"
class App(App):
def build(self):
return Launch()
if __name__ == '__main__':
App().run()
应用程序.kv:
#:kivy 1.9.1
<Launch>:
BoxLayout:
Button:
size:(80,80)
size_hint:(None,None)
text:"Click me"
on_press: say_hello
原文由 Mike Delta 发布,翻译遵循 CC BY-SA 4.0 许可协议
模式:
.kv
It’s very simple,
say_hello
belongs to theLaunch
class so in order to use it in your.kv
file you have to writeroot.say_hello
.请注意say_hello
是您要执行的函数,因此您不必忘记()
—>root.say_hello()
此外,如果
say_hello
在App
类中,您应该使用App.say_hello()
因为它属于应用程序。 (注意:即使您的 App 类是class MyFantasicApp(App):
它也总是App.say_hello()
或app.say_hello()
我不记得了,抱歉)。模式:
.py
你可以
bind
这个功能。为什么使用
bind
?对不起,不知道。但是你在 kivy 指南 中使用它。