Here is a section of documentation you should read: https://kivy.org/doc/stable/guide/events.html
The key element to conceptualize is that kivy (or any gui) is an event driven system. There is a main loop in kivy. When nothing is running, the main loop is running waiting for an event. That event could be a clock time out, a keyboard press, a mouse event, a touch event… When these events occur, the main event loop then launches your appropriate code. If you need to wait, use Clock. Never sleep in the main thread. This will prevent the kivy event loop from executing. If you execute a long loop – the event loop does not run.
You do not need to issue draw events, generally kivy will keep the screen up to date.
For your code you want to break the loop into steps. Rather than using a for loop, Create a counter instance variable. Every 'step’ count down your instance variable, and setup a call back to the same method, until the counter reaches zero (or a set value…). The example below uses this approach to create a countdown timer.
There is a second example at the bottom of this note that does some simple animation, and creates a pause between the motions.
Here is an example that creates a count down using this approach.
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.clock import Clock
kv = """
BoxLayout:
orientation: 'vertical'
Label:
text: str(app.count_down)
font_size: 80
Button:
size_hint_y: None
height: 48
text: 'Restart'
on_release:
app.count_down = 11
app.execute_count_down()
disabled: app.count_down != 0
"""
class CountDownApp(App):
count_down = NumericProperty(10)
# Kivy property used because it is being displayed,
# if the count is not displayed you could use an instance variable
def build(self):
return Builder.load_string(kv)
def on_start(self):
Clock.schedule_once(self.execute_count_down, 3)
def execute_count_down(self, *args):
if not self.count_down:
return # do not schedule a callback
else:
self.count_down -= 1
Clock.schedule_once(self.execute_count_down, 1)
CountDownApp().run()
Here is a simple example that draws 2 buttons. Click on a button and it moves to the center, pauses, and then moves back.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.properties import NumericProperty
from kivy.clock import Clock
from kivy.animation import Animation
kv = """
<CardButton>:
size_hint: None, None
size: 100, 150
on_release: self.move_card()
FloatLayout:
id: float
CardButton:
id: card_1
text: 'Card 1'
pos: 0, float.center_y - self.height/2
home_pos: 0
CardButton:
id: card_2
text: 'Card 2'
pos: float.right - self.width, float.center_y - self.height/2
home_pos: float.right - self.width
"""
class CardButton(Button):
home_pos = NumericProperty()
def move_card(self):
to_center = Animation(x=self.parent.center_x - self.width/2, duration=4) # animates the position
to_center.bind(on_complete=self._wait_move_to_home) # when animation complete, callback
to_center.start(self)
def _wait_move_to_home(self, *args):
Clock.schedule_once(self._move_to_home, 2) # wait for 2 seconds
def _move_to_home(self, *args):
to_home = Animation(x=self.home_pos, duration=2) # move back with duration 2 seconds
to_home.start(self)
class AnimCards(App):
def build(self):
return Builder.load_string(kv)
def on_start(self):
pass
AnimCards().run()
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/09649cb0-9593-4fe6-8496-daf6a6d4ade8n%40googlegroups.com.
This is too much code for me to go through. The zipped code is missing a dependency and will not run… and it is also a lot of code.
Try stepping through the code with a debugger and see what is happening.
Or share a minimal, executable example that shows your issue.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d9192eb7-c0d8-4322-ae16-d917edf2fd67n%40googlegroups.com.