Clock App with Kivy using Python
In this article, we are going to see how to develop a Clock Application with Kivy using Python.
Kivy is a graphical user interface opensource Python library that allows you to develop multi-platform applications on Windows, macOS, Android, iOS, Linux, and Raspberry-Pi. In addition to the regular mouse and keyboard inputs, it also supports multitouch events.
We will be using the time module to get the current time and update it every second. Moreover, we will be also be displaying two times of different timezones.
- asctime(): By default gets the local time. You can also pass the time zone in it to get the time of another zone as done in Example 2.
- BoxLayout(): It is used to arrange layouts in vertical or horizontal boxes.
- schedule_interval(): Is used to create time intervals and recall the function/event.
- add_widget(): Adds widget to the screen.
Example 1: Local time app using kivy
- The code starts with importing the modules that we need to use.
- The first module is time which will allow us to get the current local time.
- Next, it imports the Clock class which will be used for scheduling updates of myclock widget.
- It then creates a new app called TimeApp and builds it by adding a layout boxlayout and text label.
- The code is a sample of how to create an application with the Kivy framework.
- The first line imports modules that will allow us to get time and use it in our code.
- Line 3 declares the App class which is the base for creating Kivy applications.
- It allows us to make interval calls, add widgets, and return layouts.
- Line 4 creates a new instance of myclock widget which renders text.
- Line 5 schedules 1 second update intervals on clock1 by calling Clock.schedule_interval().
- Line 6 adds layout to screen with widgets added and returns layout as result of build() function call.
- Line 7 adds text to screen with Label widget added and returns layout as result of build() function call.
Example 2: Adding another time zone in the time app
Create another clock class and Add an extra layout for clock 2 in-app class.
- The code starts by creating two Label widgets.
- The first is myclock1 and the second is myclock2.
- They are both created with a function called update, which takes no arguments and returns nothing.
- This means that when they are updated, their text will be set to the current time in asctime().
- The next step is to create a BoxLayout widget with an orientation of ‘vertical’.
- It then adds these two Label widgets to it.
- Finally, it creates another BoxLayout widget but this one has an orientation of ‘horizontal’ and adds those two Label widgets again.
- The code is designed to create a widget that displays the current time on screen.
- The first widget, myclock1, will update every second and display the current time in text form.
- The second widget, myclock2, will update every second and display the current time in text form with an extra line of text at the top displaying the difference between seconds (in this case 1).
My Personal Notes arrow_drop_up
Please Login to comment...