Put a Clock in a BoxLayout

85 views
Skip to first unread message

Mart1

unread,
Nov 20, 2019, 5:21:13 AM11/20/19
to Kivy users support
Hi,
I want to know how to put a clock inside a BoxLayout:

For example I have this :
class IncrediblyCrudeClock(Label):
   
def update(self, *args):
       
self.text = time.asctime()

class TimeApp(App):
   
def build(self):
        crudeclock
= IncrediblyCrudeClock()
       
Clock.schedule_interval(crudeclock.update, 1)
       
return crudeclock

if __name__ == "__main__":
   
TimeApp().run()

And I want ot put it inside a layout :
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

# Create both screens. Please note the root.manager.current: this is how
# you can control the ScreenManager from kv. Each screen has by default a
# property manager that gives you the instance of the ScreenManager used.
Builder.load_string("""
#:import os os
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Goto HVAC Controller'
            on_press: root.manager.current = 'hvac'
        Button:
            text: 'Switch to Phone'

<HvacScreen>:
    temperature: temperature
    BoxLayout:
        orientation: 'vertical'
        Slider:
            min: 0
            max: 16
            step: 1
            orientation: 'horizontal'
        Label:
            text: 'temperature='+str(int(temperature.value))
        Slider:
            id: power
            min: 0
            max: 6
            step: 1
            orientation: 'horizontal'
        Label:
            text: 'Power='+str(int(power.value))
    FloatLayout:
        Button:
            text: 'Back to menu'
            on_press: app.stop()
            size_hint: .2, .1    
            pos_hint: {'x':.8, 'y':0}
"""
)


# on_press: root.manager.current = 'menu'
# Declare both screens
class MenuScreen(Screen):
   
pass


class HvacScreen(Screen):
   
pass

# Create the screen manager


sm
= ScreenManager()
sm
.add_widget(MenuScreen(name='menu'))
sm
.add_widget(HvacScreen(name='hvac'))


class TestApp(App):
   
def build(self):
       
return sm


if __name__ == '__main__':
   
TestApp().run()

Thanks ;) !

Elliot Garbus

unread,
Nov 20, 2019, 6:55:19 PM11/20/19
to Kivy users support
2 files attached with changes:
mart_clock.py your clock 
mart_layout.py the rest of your code

comments:
In mart_clock.py  I've reorganized your code so you can use this file as a strand alone to test your clock implementation, or import it into another file to use. 
I have also moved the creation of the schedule into the constructor (the __init__ method), and out of the test code.

In mart_layout.py I imported the clock code into the kv code.  I added the IncrediblyCrudeClock in a BoxLayout.    I also moved the screen manager to kv, and added the temperate id to the first slider to get the code to build.
mart_clock.py
mart_layout.py

Mart1

unread,
Nov 24, 2019, 9:46:33 AM11/24/19
to Kivy users support
Thanks !!
Ok for the the clock.py but I have an error for the layout.py :
 Traceback (most recent call last):
   
File "C:/Users/Martin/PycharmProjects/Hvac/mart_layout.py", line 84, in <module>
     
TestApp().run()
   
File "C:\Python37\lib\site-packages\kivy\app.py", line 800, in run
     root
= self.build()
   
File "C:/Users/Martin/PycharmProjects/Hvac/mart_layout.py", line 80, in build
     
return Builder.load_string(kv)
   
File "C:\Python37\lib\site-packages\kivy\lang\builder.py", line 368, in load_string
     parser
= Parser(content=string, filename=fn)
   
File "C:\Python37\lib\site-packages\kivy\lang\parser.py", line 392, in __init__
     
self.parse(content)
   
File "C:\Python37\lib\site-packages\kivy\lang\parser.py", line 498, in parse
     
self.execute_directives()
   
File "C:\Python37\lib\site-packages\kivy\lang\parser.py", line 462, in execute_directives
     mod
= __import__(package)
   
File "C:\Users\Martin\PycharmProjects\Hvac\mart_clock.py", line 29, in <module>
     
class TimeApp(App):
 
NameError: name 'App' is not defined
 which says me that "App" is not defines...which is strange I imported it just before in the code...What's wrong here ?

Elliot Garbus

unread,
Nov 24, 2019, 9:54:42 AM11/24/19
to kivy-...@googlegroups.com

Did you edit something?  Are both files located in the same directory?

--
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/5447b4a5-e97d-410c-ab95-5b046274c6f9%40googlegroups.com.

 

Mart1

unread,
Nov 24, 2019, 9:59:43 AM11/24/19
to Kivy users support
The problem seems to be with the import of this in my kivy file which raise this issue :
#:import IncrediblyCrudeClock mart_clock.IncrediblyCrudeClock


Mart1

unread,
Nov 24, 2019, 10:01:21 AM11/24/19
to Kivy users support
Nop and yes both in the same directory

Elliot Garbus

unread,
Nov 24, 2019, 10:04:08 AM11/24/19
to kivy-...@googlegroups.com

The problem seems to be:

File "C:\Users\Martin\PycharmProjects\Hvac\mart_clock.py", line 29, in <module>
     
class TimeApp(App):

This line of code should not be executed, if being imported.  It is under the test for __name__

These files work for me.  Put a print statement under this if statement.  This code should not be getting executed if you are not running from mart_clock.py.

 

How are you invoking the code?

 

if __name__ == "__main__":
   
from kivy.app import App
   
from kivy.lang import Builder

    kv=
"""
BoxLayout:
    orientation: 'vertical'
    Label:
        text: 'Clock Test'
    IncrediblyCrudeClock:
        font_size: '50sp'
"""

   
class TimeApp(App):
       
def build(self):
           
return Builder.load_string(kv)

    TimeApp().run()

 

 

 

 

From: Mart1
Sent: Sunday, November 24, 2019 7:59 AM
To: Kivy users support
Subject: [kivy-users] Re: Put a Clock in a BoxLayout

 

The problem seems to be with the import of this in my kivy file which raise this issue :

--

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.

Elliot Garbus

unread,
Nov 24, 2019, 10:06:42 AM11/24/19
to kivy-...@googlegroups.com

just run:

python mart_layout.py

 

From: Elliot Garbus
Sent: Sunday, November 24, 2019 8:04 AM
To: kivy-...@googlegroups.com
Subject: RE: [kivy-users] Re: Put a Clock in a BoxLayout

 

The problem seems to be:

File "C:\Users\Martin\PycharmProjects\Hvac\mart_clock.py", line 29, in <module>
     
class TimeApp(App):

This line of code should not be executed, if being imported.  It is under the test for __name__

These files work for me.  Put a print statement under this if statement.  This code should not be getting executed if you are not running from mart_clock.py.

 

How are you invoking the code?

 

if __name__ == "__main__":
   
from kivy.app import App
   
from kivy.lang import Builder

    kv=
"""

BoxLayout:
    orientation: 'vertical'
    Label:
        text: 'Clock Test'
    IncrediblyCrudeClock:
        font_size: '50sp'
"""

   
class TimeApp(App):
       
def build(self):
           
return Builder.load_string(kv)

    TimeApp().run()

 

 

 

 

From: Mart1
Sent: Sunday, November 24, 2019 7:59 AM
To: Kivy users support
Subject: [kivy-users] Re: Put a Clock in a BoxLayout

 

The problem seems to be with the import of this in my kivy file which raise this issue :

--

Mart1

unread,
Nov 24, 2019, 10:10:13 AM11/24/19
to Kivy users support
Ok I copied at the top of my clock.py :
    from kivy.app import App
   
from kivy.lang import Builder

And now I have this:

issue_1.PNG


and no more this :

issue_1_1.PNG

and then this :

issue_1_2.PNG

Mart1

unread,
Nov 24, 2019, 10:19:10 AM11/24/19
to Kivy users support
Yeah I just did "python mart_layout" and it raised me this error...I just download them and it raised me this.

Elliot Garbus

unread,
Nov 24, 2019, 10:42:54 AM11/24/19
to kivy-...@googlegroups.com

When I run mart_layout.py:

 

When I run clock.py

 

 

 

 

From: Mart1
Sent: Sunday, November 24, 2019 8:19 AM
To: Kivy users support
Subject: [kivy-users] Re: Put a Clock in a BoxLayout

 

Yeah I just did "python mart_layout" and it raised me this error...I just download them and it raised me this.

--

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.

Elliot Garbus

unread,
Nov 24, 2019, 10:47:50 AM11/24/19
to kivy-...@googlegroups.com

Check the formatting at the bottom of mart_clock.py

 

Perhaps something reformatted the code?  It should look just like below.  The code must all be indented under the if..

 

if __name__ == "__main__":
   
from kivy.app import App
   
from kivy.lang import Builder

    kv=
"""

BoxLayout:
    orientation: 'vertical'
    Label:
        text: 'Clock Test'
    IncrediblyCrudeClock:
        font_size: '50sp'
"""

   
class TimeApp(App):
       
def build(self):
           
return Builder.load_string(kv)

    TimeApp().run()

 

 

From: Elliot Garbus
Sent: Sunday, November 24, 2019 8:42 AM
To: kivy-...@googlegroups.com

Subject: RE: [kivy-users] Re: Put a Clock in a BoxLayout

 

When I run mart_layout.py:

cid:image003.png@01D5A2A3.20457710

 

When I run clock.py

 

cid:image006.png@01D5A2A3.20457710

Mart1

unread,
Nov 24, 2019, 11:15:35 AM11/24/19
to Kivy users support
I managed ! I just re-download them and it was okay I probably did some changes without paying attention.
If I want to put this one :
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line
from kivy.uix.floatlayout import FloatLayout
from math import cos, sin, pi
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import NumericProperty

import datetime

kv
= '''
#:import math math

[ClockNumber@Label]:
    text: str(ctx.i)
    pos_hint: {"center_x": 0.5+0.42*math.sin(math.pi/6*(ctx.i-12)), "center_y": 0.5+0.42*math.cos(math.pi/6*(ctx.i-12))}
    font_size: self.height/16

<MyClockWidget>:
    face: face
    ticks: ticks
    FloatLayout:
        id: face
        size_hint: None, None
        pos_hint: {"center_x":0.5, "center_y":0.5}
        size: 0.9*min(root.size), 0.9*min(root.size)
        canvas:
            Color:
                rgb: 0.1, 0.1, 0.1
            Ellipse:
                size: self.size    
                pos: self.pos
        ClockNumber:
            i: 1
        ClockNumber:
            i: 2
        ClockNumber:
            i: 3
        ClockNumber:
            i: 4
        ClockNumber:
            i: 5
        ClockNumber:
            i: 6
        ClockNumber:
            i: 7
        ClockNumber:
            i: 8
        ClockNumber:
            i: 9
        ClockNumber:
            i: 10
        ClockNumber:
            i: 11
        ClockNumber:
            i: 12
    Ticks:
        id: ticks
        r: min(root.size)*0.9/2
'''

Builder.load_string(kv)

class MyClockWidget(FloatLayout):
   
pass

class Ticks(Widget):
   
def __init__(self, **kwargs):
       
super(Ticks, self).__init__(**kwargs)
       
self.bind(pos=self.update_clock)
       
self.bind(size=self.update_clock)

   
def update_clock(self, *args):
       
self.canvas.clear()
       
with self.canvas:
            time
= datetime.datetime.now()
           
Color(0.2, 0.5, 0.2)
           
Line(points=[self.center_x, self.center_y, self.center_x+0.8*self.r*sin(pi/30*time.second), self.center_y+0.8*self.r*cos(pi/30*time.second)], width=1, cap="round")
           
Color(0.3, 0.6, 0.3)
           
Line(points=[self.center_x, self.center_y, self.center_x+0.7*self.r*sin(pi/30*time.minute), self.center_y+0.7*self.r*cos(pi/30*time.minute)], width=2, cap="round")
           
Color(0.4, 0.7, 0.4)
            th
= time.hour*60 + time.minute
           
Line(points=[self.center_x, self.center_y, self.center_x+0.5*self.r*sin(pi/360*th), self.center_y+0.5*self.r*cos(pi/360*th)], width=3, cap="round")

class MyClockApp(App):
   
def build(self):
        clock
= MyClockWidget()
       
Clock.schedule_interval(clock.ticks.update_clock, 1)
       
return clock

if __name__ == '__main__':
   
MyClockApp().run()
It will be the same method ?
Thanks again mate you're really help me that's cool !

Le dimanche 24 novembre 2019 16:42:54 UTC+1, Elliot Garbus a écrit :

When I run mart_layout.py:

 

When I run clock.py

 

 

 

 

From: Mart1
Sent: Sunday, November 24, 2019 8:19 AM
To: Kivy users support
Subject: [kivy-users] Re: Put a Clock in a BoxLayout

 

Yeah I just did "python mart_layout" and it raised me this error...I just download them and it raised me this.

--
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-...@googlegroups.com.

Elliot Garbus

unread,
Nov 24, 2019, 12:06:51 PM11/24/19
to kivy-...@googlegroups.com

Yes, the same method should work.

Take notice, templates : [ClockNumber@Label]
have been deprecated in kivy.  I suggest updating this to not use templates.

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/22c6ec5a-ba7c-4802-a152-2e472c04aa50%40googlegroups.com.

 

Elliot Garbus

unread,
Nov 24, 2019, 12:21:34 PM11/24/19
to kivy-...@googlegroups.com

Use this instead of the template:

 

<ClockNumber@Label>:
    i: 0
    text: str(self.i)
    pos_hint: {"center_x": 0.5+0.42*math.sin(math.pi/6*(self.i-12)), "center_y": 0.5+0.42*math.cos(math.pi/6*(self.i-12))}
    font_size: self.height/16

 

 

From: Elliot Garbus
Sent: Sunday, November 24, 2019 10:06 AM
To: kivy-...@googlegroups.com

Mart1

unread,
Nov 24, 2019, 12:39:51 PM11/24/19
to Kivy users support
I tried this :
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line
from kivy.uix.floatlayout import FloatLayout
from math import cos, sin, pi
from kivy.clock import Clock
from kivy.properties import NumericProperty
import datetime


class MyClockWidget(FloatLayout):
   
def __init__(self, **kwargs):
       
Clock.schedule_interval(self.ticks.update_clock, 1)
       
super().__init__(**kwargs)


if __name__ == "__main__":
   
from kivy.app import App
   
from kivy.lang import Builder


   
class MyClockApp(App):
       
def build(self):
           
return Builder.load_string(kv)



   
class Ticks(Widget):
       
def __init__(self, **kwargs):
           
super(Ticks, self).__init__(**kwargs)
           
self.bind(pos=self.update_clock)
           
self.bind(size=self.update_clock)

       
def update_clock(self, *args):
           
self.canvas.clear()
           
with self.canvas:
                time
= datetime.datetime.now()
               
Color(0.2, 0.5, 0.2)
               
Line(points=[self.center_x, self.center_y, self.center_x + 0.8 * self.r * sin(pi / 30 * time.second),

                             
self.center_y + 0.8 * self.r * cos(pi / 30 * time.second)], width=1, cap="round")

               
Color(0.3, 0.6, 0.3)
               
Line(points=[self.center_x, self.center_y, self.center_x + 0.7 * self.r * sin(pi / 30 * time.minute),

                             
self.center_y + 0.7 * self.r * cos(pi / 30 * time.minute)], width=2, cap="round")
               
Color(0.4, 0.7, 0.4)
                th
= time.hour * 60 + time.minute
               
Line(points=[self.center_x, self.center_y, self.center_x + 0.5 * self.r * sin(pi / 360 * th),
                             
self.center_y + 0.5 * self.r * cos(pi / 360 * th)], width=3, cap="round")

   
MyClockApp().run()

But I have nothing.
Can you please correct me to get it work or give me the same two files that you did with the other example (Then I will see what did you do to do it again myself) ? Thanks again.

Elliot Garbus

unread,
Nov 24, 2019, 12:47:07 PM11/24/19
to kivy-...@googlegroups.com

The definition of the class Ticks and it’s methods need to be above the if __name__ == ‘__main__’:

Cut and paste this code toward the top of the file.  Do you understand why?

 

From: Mart1
Sent: Sunday, November 24, 2019 10:40 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: Put a Clock in a BoxLayout

 

I tried this :

--

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.

Mart1

unread,
Nov 24, 2019, 1:23:25 PM11/24/19
to Kivy users support
Yeah I agree with you and I did this at first but it didn't work so I tried another thing which is the code I paste the post above.
But yeah I agree. So let's get back to what I thought the good way at first:
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line
from kivy.uix.floatlayout import FloatLayout
from math import cos, sin, pi
from kivy.clock import Clock
from kivy.properties import NumericProperty
import datetime


class MyClockWidget(FloatLayout):
   
def __init__(self, **kwargs):
       
Clock.schedule_interval(self.ticks.update_clock, 1)
       
super().__init__(**kwargs)


class Ticks(Widget):
   
def __init__(self, **kwargs):
       
super(Ticks, self).__init__(**kwargs)
       
self.bind(pos=self.update_clock)
       
self.bind(size=self.update_clock)

   
def update_clock(self, *args):
       
self.canvas.clear()
       
with self.canvas:
            time
= datetime.datetime.now()
           
Color(0.2, 0.5, 0.2)
           
Line(points=[self.center_x, self.center_y, self.center_x + 0.8 * self.r * sin(pi / 30 * time.second),
                         
self.center_y + 0.8 * self.r * cos(pi / 30 * time.second)], width=1, cap="round")
           
Color(0.3, 0.6, 0.3)
           
Line(points=[self.center_x, self.center_y, self.center_x + 0.7 * self.r * sin(pi / 30 * time.minute),
                         
self.center_y + 0.7 * self.r * cos(pi / 30 * time.minute)], width=2, cap="round")
           
Color(0.4, 0.7, 0.4)
            th
= time.hour * 60 + time.minute
           
Line(points=[self.center_x, self.center_y, self.center_x + 0.5 * self.r * sin(pi / 360 * th),
                         
self.center_y + 0.5 * self.r * cos(pi / 360 * th)], width=3, cap="round")


if __name__ == "__main__":
   
from kivy.app import App
   
from kivy.lang import Builder

    kv
=
"""
#:import math math

<ClockNumber@Label>:
    i: 0
    text: str(self.i)
    pos_hint: {"
center_x": 0.5+0.42*math.sin(math.pi/6*(self.i-12)), "center_y": 0.5+0.42*math.cos(math.pi/6*(self.i-12))}
center_x":0.5, "center_y":0.5}



   
MyClockApp().run()

But I have this error :
[CRITICAL] [Application ] No window is created. Terminating application run.

Elliot Garbus

unread,
Nov 24, 2019, 1:28:58 PM11/24/19
to kivy-...@googlegroups.com

You need to instance MyClockWidget in you kv code for the test.  At the bottom of the KV code in the file below add:

 

MyClockWidget:

 

This will instance the clock widget. 

 

From: Mart1
Sent: Sunday, November 24, 2019 11:23 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: Put a Clock in a BoxLayout

 

Yeah I agree with you and I did this at first but it didn't work so I tried another thing which is the code I paste the post above.

--

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.

Mart1

unread,
Nov 24, 2019, 2:17:56 PM11/24/19
to Kivy users support
Okay, I supposse that I need to put some properties in it nah ? Otherwise still get nothing :( (Sorry I'm not an expert as you can see so I'm trying to learn this language by the help of others, forums and all)
":0.5}
    MyClockWidget:
"""


Elliot Garbus

unread,
Nov 24, 2019, 3:47:35 PM11/24/19
to kivy-...@googlegroups.com

No need to add any properties,  MyClockWidget: needed to be all the way to the left.  It is the root widget – but that was not quite the right answer.

That will make allow the clock file to run – but is not the complete answer. 

 

The ‘rules in KV’ need to be part of the ‘base’ code for the widget, and the kv code needs to get loaded.  It is easier to show it, blow.  This will let you import the file and use the widget.  Happy to answer you questions.

 

from kivy.uix.widget import Widget
from kivy.graphics import Color, Line
from math import cos, sin, pi
from kivy.clock import Clock
from kivy.lang import Builder

import datetime

kv =
'''

#:import math math

<ClockNumber@Label>:
    i: 0
    text: str(self.i)
    pos_hint: {"center_x": 0.5+0.42*math.sin(math.pi/6*(self.i-12)), "center_y": 0.5+0.42*math.cos(math.pi/6*(self.i-12))}
    font_size: self.height/16

<MyClockWidget@FloatLayout>:
'''

Builder.load_string(kv)


class Ticks(Widget):
   
def __init__(self, **kwargs):
       
super(Ticks, self).__init__
(**kwargs)
        Clock.schedule_interval(
self.update_clock, 1)
       
self.bind(pos=self.update_clock)
       
self.bind(size=self.update_clock)

   
def update_clock(self, *args):
       
self.canvas.clear()
       
with self.canvas:
            time = datetime.datetime.now()
            Color(
0.2, 0.5, 0.2)
            Line(
points=[self.center_x, self.center_y, self.center_x+0.8*self.r*sin(pi/30*time.second), self.center_y+0.8*self.r*cos(pi/30*time.second)], width=1, cap="round")
            Color(
0.3, 0.6, 0.3)
            Line(
points=[self.center_x, self.center_y, self.center_x+0.7*self.r*sin(pi/30*time.minute), self.center_y+0.7*self.r*cos(pi/30*time.minute)], width=2, cap="round")
            Color(
0.4, 0.7, 0.4)
            th = time.hour*
60 + time.minute
            Line(
points=[self.center_x, self.center_y, self.center_x+0.5*self.r*sin(pi/360*th), self.center_y+0.5*self.r*cos(pi/360*th)], width=3, cap="round")

if __name__ == '__main__':
   
from kivy.app import
App
   
    kv_test =
"""
BoxLayout:
    orientation: 'vertical'
    Label:
        text: 'Analog Clock Test'
    MyClockWidget:
"""


   
class MyClockApp(App):
       
def build(self):
           
return Builder.load_string(kv_test)


    MyClockApp().run()

 

 

From: Mart1
Sent: Sunday, November 24, 2019 12:18 PM
To: Kivy users support
Subject: Re: [kivy-users] Re: Put a Clock in a BoxLayout

 

Okay, I supposse that I need to put some properties in it nah ? Otherwise still get nothing :( (Sorry I'm not an expert as you can see so I'm trying to learn this language by the help of others, forums and all)

--

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.

Mart1

unread,
Nov 24, 2019, 5:17:55 PM11/24/19
to Kivy users support
Awesome that's perfect almost working like a charm !!
I imported it with :
#:import IncrediblyCrudeClock simple_clock.IncrediblyCrudeClock
#:import MyClockWidget clock.Ticks
<MenuScreen@Screen>:
   
BoxLayout:
        orientation
: 'vertical'
       
BoxLayout:
            size_hint_y
: 8

           
Button:
                text
: 'Goto HVAC Controller'
                on_press
: root.manager.current = 'hvac'
           
Button:
                text
: 'Switch to Phone'

                on_press
: app.stop()

       
MyClockWidget:
       
       
IncrediblyCrudeClock:
            size_hint_y
: 2
            font_size
: '50sp'

Something strange happens. 

issue_2.PNG

As you can see the background of the clock is at the right position but not the clock pointer (wich is at the bottom)...Where is the problem here ?

Elliot Garbus

unread,
Nov 24, 2019, 7:03:23 PM11/24/19
to kivy-...@googlegroups.com

There was a problem with the way the analog clock was implemented.  The hands were not drawn relative to the face.  I updated the clock file.

 

Notice that in the original code the hands were drawn relative to self.  Now they are drawn relative to the parent.  The parent is the clock face (the parent widget).

I set p = self.parent to reduce the total amount of typing.

 

def update_clock(self, *args):
   
self.canvas.clear()
    p =
self.parent
   
with self.canvas:
        time = datetime.datetime.now()
        Color(
0.2, 0.5, 0.2)
        Line(
points=[p.center_x, p.center_y, p.center_x+0.8*self.r*sin(pi/30*time.second), p.center_y+0.8*self.r*cos(pi/30*time.second)], width=1, cap="round")
        Color(
0.3, 0.6, 0.3)
        Line(
points=[p.center_x, p.center_y, p.center_x+0.7*self.r*sin(pi/30*time.minute), p.center_y+0.7*self.r*cos(pi/30*time.minute)], width=2, cap="round")
        Color(
0.4, 0.7, 0.4)
        th = time.hour*
60 + time.minute
        Line(
points=[p.center_x, p.center_y, p.center_x+0.5*self.r*sin(pi/360*th), p.center_y+0.5*self.r*cos(pi/360*th)], width=3, cap="round")

 

The analog clock is a float layout, so you can put it anywhere. 

MyClockWidget:
    size_hint: None, None
    size: 200, 200
    pos_hint:{'center_x': .5, 'center_y': .5 }

 

 

 

 

 

From: Mart1
Sent: Sunday, November 24, 2019 3:18 PM
To: Kivy users support
Subject: Re: [kivy-users] Re: Put a Clock in a BoxLayout

 

Awesome that's perfect almost working like a charm !!

I imported it with :

#:import IncrediblyCrudeClock simple_clock.IncrediblyCrudeClock
#:import MyClockWidget clock.Ticks
<MenuScreen@Screen>:
   
BoxLayout:
        orientation
: 'vertical'
       
BoxLayout:
            size_hint_y
: 8
           
Button:
                text
: 'Goto HVAC Controller'
                on_press
: root.manager.current = 'hvac'
           
Button:
                text
: 'Switch to Phone'
                on_press
: app.stop()

       
MyClockWidget:
       
       
IncrediblyCrudeClock:
            size_hint_y
: 2
            font_size
: '50sp'


Something strange happens. 

As you can see the background of the clock is at the right position but not the clock pointer (wich is at the bottom)...Where is the problem here ?

--

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.

Elliot Garbus

unread,
Nov 24, 2019, 7:07:10 PM11/24/19
to Kivy users support