Sie können nur Python oder kv-Sprache verwenden. Das liegt ganz bei Ihnen. In diesem Fall machen wir die Tasten Aufruf Funktion, erhöhen Sie die Beschriftung Text. Ich werde zwei Beispiele machen. Eins nur mit Python und eins in Verbindung mit Kivy-Sprache. nur
Dies ist ein Beispiel in Python:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
class Game(BoxLayout):
def __init__(self,**kwargs):
super(Game,self).__init__(**kwargs)
self.count = 0
self.orientation = "vertical"
self.button = Button(on_press=self.increment, text="Increment")
self.label = Label(text="0")
self.add_widget(self.button)
self.add_widget(self.label)
def increment(self,*args):
self.count += 1
self.label.text = str(self.count)
class MyApp(App):
def build(self):
return Game()
Und gleiche App mit Python und Kivy Sprache.
Python-Datei:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
class Game(BoxLayout):
label_text = StringProperty()
def __init__(self,**kwargs):
super(Game,self).__init__(**kwargs)
self.count = 0
self.label_text = str(self.count)
def increment(self,*args):
self.count += 1
self.label_text = str(self.count)
print self.label_text
class MyApp(App):
def build(self):
return Game()
MyApp().run()
Und my.kv Datei:
#:kivy 1.9.1
<Game>:
orientation: "vertical"
Button:
text: "Increment"
on_press: root.increment()
Label:
text: root.label_text