Using the kivy Clock to change the screen

205 views
Skip to first unread message

Xavier Biancardi

unread,
Aug 2, 2019, 3:59:39 PM8/2/19
to Kivy users support
I'm fairly new with Kivy and am trying to create what is essentially a screensaver for my app. I am currently trying to set up a kivy clock to set the screen manager's current page to the screensaver one.

Below are my screen and screenmanager classes, the MyScreenManager class has the change_screen function which is the one that changes the current screen to the screensaver. I have tested the functionality of the change_screen function by calling it with a button and it worked fine.

However, when I call the function with the Clock.schedule_one(..., ...) it fails and gives me this error:

self.add_widget(ss)
AttributeError: 'float' object has no attribute 'add_widget'

Looked online and couldn't find any definitive solution, any help would be awesome!

Thanks,

Xavier

Alexander Taylor

unread,
Aug 2, 2019, 4:40:32 PM8/2/19
to Kivy users support
 You schedule the change_screen function at class level, not instance level, so it receives no implicit self argument (effectively you're calling it as a normal function). It automatically receives an argument from Clock.schedule_once, the argument is the time since the scheduling or something like that. That argument is a float, so self is a float, so you get the error you see.

The code doesn't really make sense as it stands, please provide a minimal runnable example demonstrating where you want to add functionality but can't work out how.

Xavier Biancardi

unread,
Aug 2, 2019, 5:04:08 PM8/2/19
to Kivy users support
Thanks for your reply Alexander,

Below is a link to the code, the Clock.schedule_once has been commented out so that it can load. Essentially my current goal is to create a screensaver that after say 20 seconds without user input the app changes the screen to a screen with a company logo or something (currently just the screen titled ScreenSaver).


Thanks

Xavier Biancardi

unread,
Aug 6, 2019, 6:19:12 PM8/6/19
to Kivy users support
Just following up on your previous reply. I am currently trying to do two things: create a screensaver so that if there is no user input for 20s it changes screen to a desired screensaver, and secondly to have the needle automatically rotate a bit every couple seconds.

I am fairly new to Kivy so it is entirely possible that I am missing something obvious but I just can't seem to call the Clock.schedule_once automatically on startup and not through a button.

Additionally I have been trying to have a value (dash_val) increase by 10 every 2 seconds so that the needle rotates automatically as a test, however, in a similar vein to the screensaver I can't figure out how the clock is/isn't working.


Sorry if my questions seem a bit vague, I can try to clarify if needed.

Thanks.

On Friday, August 2, 2019 at 1:40:32 PM UTC-7, Alexander Taylor wrote:

Elliot Garbus

unread,
Aug 6, 2019, 9:05:38 PM8/6/19
to kivy-...@googlegroups.com

Here is a simple example I helped another user with that shows how to use Clock.schedule_interval.  You can use Clock.schedule_once the same way.  Code attached.

 

 

class SimClock(Label):
    current_time = StringProperty(strftime(
'%H:%M:%S'))

   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.timer = Clock.schedule_interval(self.update_time, 1)

   
def update_time(self, dt):
       
self.current_time = strftime('%H:%M:%S')

   
def cancel_time(self):
       
self.timer.cancel()

 

There is a class SimClock derived from a label.  When the class is constructed, the schedule is started.

Once a second update_time() is called.  It reads the current time and updates the value on the label, using the string property current time.

 

Looking at part of the KV code…

 

ImageButton:
   
size_hint_x: 1
   
size_hint_y: 0.8
   
source: './clock.jpeg'
   
on_release:
        mylabel.cancel_time()
       
self.source = './play.png'
       
mylabel.text = 'wakey wakey eggs and beaky'

 

When the mouse button is released on the clock image, the cancel_time() method is called, stopping the schedule.

--
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/46de4ef8-1b89-4b64-89bb-b589dbf1f4b8%40googlegroups.com.

 

myclock.kv
myclock.py
play.png
clock.jpeg
Reply all
Reply to author
Forward
0 new messages