Forum Archive

[RESOLVED!] Help with scene timing (short code)

Pythonistapro777

How can I make desp (scene) be run 10 seconds after the first click of the button?

Thanks in advance!

Here's the code:


import time
from scene import *
from PIL import Image
clicks = 0
class JitterClick(Scene):
    def setup(self):
        self.button = Button(Rect(self.size.w/2-100, self.size.h/2-140, 200, 200))
        self.button.background = Color(0,0,0)
        self.button.stroke = Color(0,0,0)
        self.button.image = 'Red_Circle'
        self.button.action = self.add_clicks
        self.add_layer(self.button)

    def add_clicks(sender):
        global clicks
        clicks += 1

    def draw(self):
        background(0,0,0)
        self.button.background = Color(0,0,0)
        self.button.draw()
        text('Clicks: %i' % clicks, x=self.size.w/2, y=self.size.h/3.8*3, font_size=57)

class desp(Scene):
    def setup(self):
        self.show_instructions = True
        self.p_size = 64 if self.size.w > 700 else 32

    def draw(self):
        background(0, 0, 0)
        speed=int(clicks)/10
        text('In 10 seconds you managed to click %i times. At a speed of 5 clicks a second.' % clicks, x=self.size.w/2, y=self.size.h/3.8*3, font_size=57)

run(JitterClick())
ccc

In general, there is only supposed to be one scene.Scene in your app. It can be quite tricky to work around this limit but it can be done.

You can search the forum for "multiscene" for one approach to getting around this limitation. You can see https://omz-forums.appspot.com/pythonista/post/5876387541942272 for a second approach.

Are you better off using ui instead of scene or if you NEED a scene consider embedding it inside a ui.SceneView which will give you more flexibility. https://omz-forums.appspot.com/pythonista/post/5290516186923008

Pythonistapro777

Ok. I decided to do completely different, instead of making that I count the number of clicks in 10 seconds, I'll just record the time taken to do 100 clicks.
Thanks for the help anyways @ccc