Python Kivy:如何在按钮点击时调用函数? [英] Python Kivy: how to call a function on button click?
问题描述
我在使用kivy库方面很新颖。
我有一个app.py文件和一个app.kv文件,我的问题是我无法在按钮按下时调用某个函数。
app.py:
从kivy.app导入kivy
导入App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
$ b $ class Launch(BoxLayout):
def __init __(self,** kwargs) :
super(Launch,self).__ init __(** kwargs)
def say_hello(self):
printhello
class App(App):
def build(self):
return Launch()
if __name__ =='__main__':
App()。run()
app.kv:
#:kivy 1.9.1
:
BoxLayout:
Button:
size :(80,80)
size_hint :(无,无)
文字:Click me
on_press:say_hello
模式: .kv
非常简单, say_hello
属于 Launch
类,因此为了在 .kv
文件中使用它必须写 root.say_hello
。注意 say_hello
是一个你想要执行的函数,所以你不必忘记()
- - > root.say_hello()
。
另外,如果 say_hello
出现在 App
类中,应该使用 App.say_hello()
,因为它属于应用程序。 (注意:即使你的App类是 class MyFantasicApp(App):
它总是会是 App.say_hello()
或 app.say_hello()
我不记得,对不起)。
#:kivy 1.9.1
<启动>:
BoxLayout:
按钮:
大小:(80,80)
size_hint: (None,None)
text:Click me
on_press:root.say_hello()
模式: .py
您可以绑定
函数。
from kivy.uix.button import Button#你需要更多的这个
class Launch (BoxLayout):
def __init __(self,** kwargs):
super(Launch,self).__ init __(** kwargs)
mybutton = Button(
text ='点击我',
size =(80,80),
size_hint =(无,无)
)
mybutton.bind(on_press = self.say_hello)#注意:这里say_hello没有括号。
Launch.add_widget(mybutton)
def say_hello(self):
printhello
为什么使用 bind
?对不起,不知道。但是你在 kivy指南中使用它。
i'm pretty new at using kivy library.
I have an app.py file and an app.kv file , my problem is that I can't call a function on button press.
app.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()
app.kv:
#:kivy 1.9.1
<Launch>:
BoxLayout:
Button:
size:(80,80)
size_hint:(None,None)
text:"Click me"
on_press: say_hello
Mode:.kv
It's very simple, say_hello
belongs to the Launch
class so in order to use it in your .kv
file you have to write root.say_hello
. Note that say_hello
is a function that you want to execute so you don't have to forget the ()
---> root.say_hello()
.
Also, if say_hello
were in App
class you should use App.say_hello()
because it belongs to the app. (Note: even if your App class were class MyFantasicApp(App):
it would always be App.say_hello()
or app.say_hello()
I don't remember, sorry).
#:kivy 1.9.1
<Launch>:
BoxLayout:
Button:
size:(80,80)
size_hint:(None,None)
text:"Click me"
on_press: root.say_hello()
Mode: .py
You can bind
the function.
from kivy.uix.button import Button # You would need futhermore this
class Launch(BoxLayout):
def __init__(self, **kwargs):
super(Launch, self).__init__(**kwargs)
mybutton = Button(
text = 'Click me',
size = (80,80),
size_hint = (None,None)
)
mybutton.bind(on_press = self.say_hello) # Note: here say_hello doesn't have brackets.
Launch.add_widget(mybutton)
def say_hello(self):
print "hello"
Why use bind
? Sorry, no idea. But you it's used in the kivy guide.
这篇关于Python Kivy:如何在按钮点击时调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!