Clock using KIVY Python

This blog discusses simple clock project using kivy and Python. This app is a simple UI clock app.

To build this app, I have used two widgets BoxLayout and Label. BoxLayout is a one dimension widget which contain Label as a child widget. Layout for this app is build using Builder string.

Below modules imported for this app.

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from datetime import datetime
from kivy.clock import Clock

Kivy clock module for calling a function every 1 second and updating the time on the clock app. Importing datetime module to use strftime function in code. Importing BoxLayout and Label from kivy.

Creating string which contains BoxLayout and Label.

kv ='''
BoxLayout:
    orientation:'vertical'
    padding:40
    spacing:20
    Label:
        id:time_day
        font_name:'Impact'
        markup:True
        font_size:80
        background_color:0,0,0,0
        halign:'center'
        multiline:True

'''

In the below program, creating ClockApp() class with App as a base class and displaying content of builder string using return statement.

Method on_start() has clock schedule which calls method update() every 1 second. Method update() display day and time on the Label text. Used markup for applying color, size and making text bold. Set markup property to True to use it on our program. In the above kivy string, we have set markup property to True inside Label widget.

Strfime() function :- Represent date and time in string format. It takes one or more formatted code and returns string representation. Syntax : – strftime(format). It requires datetime module

Below formatted code use in our program.

  • $A -> Display Full Weekday
  • %d -> Display day of the month
  • %B -> Full month name
  • %Y -> Year
  • %I -> Hour in 12 format
  • %M -> Minutes
  • %S -> Seconds

Displaying weekday on the first line, date on the second line and time at the bottom. Used \n new line character.

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

    def on_start(self):
        Clock.schedule_interval(self.update,1)

    def update(self,nap):
        self.day = '[color=#a84232]'+'[size=120]'+strftime('%A')+'[/size]'+'[/color]'
        self.date= '[color=#85a832]'+'[size=90]'+strftime('%d %B, %Y')+'[/size]'+'[/color]'
        self.time = '[color=#9e3898]'+'[size=80]'+strftime('%I:%M:%S')+'[/size]'+'[/color]'
        self.root.ids.time_day.text = '[b]'+self.day+'\n'+self.date+'\n'+self.time+'[/b]'




ClockApp().run()

Output

clock

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s