Writing A Simple Kivy-based CPU-Monitoring App in Python

Kanishka V. Namdeo
4 min readApr 12, 2020

--

So what is Python?

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.

It is used for:

  1. Software Development
  2. Web Development
  3. Mathematics
  4. And mostly everything else!

What is Kivy?

Kivy is an open source, cross-platform Python framework for the development of applications that make use of innovative, multi-touch user interfaces.

Kivy is written in Python and Cython, based on OpenGL ES 2, supports various input devices and has an extensive widget library. With the same codebase, you can target Windows, macOS, Linux, Android and iOS.

What is KivyMD?

KivyMD is a collection of Material Design compliant widgets for use with Kivy.

Before we get started!

Here are some basic kivy terminologies:

  1. App: The App class is the base for creating Kivy applications. Think of it as your main entry point into the Kivy run loop. In most cases, you subclass this class and make your own app. You create an instance of your specific app class and then, when you are ready to start the application’s life cycle, you call your instance’s App.run() method.
  2. Layout: Layouts are containers used to arrange widgets in a particular manner. There are several types of layouts. We will be using a few of them.
  3. Label: The Label widget is for rendering text. It supports ascii and unicode strings

Also, this app will be using a lot of Object Oriented Programming and the use of threads (Remember kids, practice safe S̶e̶x̶ /threads! 😉)

So here’s some refresher courses for them:

https://realpython.com/python3-object-oriented-programming/

https://realpython.com/intro-to-python-threading/

Let’s begin!

  1. We are using a python 3 based environment for this. Use Pip or Anaconda for package management alternatively or simlutaneously, but remember not to mess up the package dependencies!
  2. If you’re using pip
pip install kivy                                   #the UI framework
pip install kivymd #for material design
pip install psutil #for system parameters

3. If you’re using Anaconda

conda install -c conda-forge kivy
conda install -c conda-forge psutil

Since Anaconda doesn’t have kivymd, stick to the pip command to install it.

4. We create a new python file and import the following packages

Yes, you must be thinking what the Config.set does.

Configuration options control the initialization of the App. In order to avoid situations where the config settings do not work or are not applied before window creation (like setting an initial window size), Config.set should be used before importing any other Kivy modules. Ideally, this means setting them right at the start of your main.py script.

5. Next, we create some global variables that are going to be passed around

6. The design part of the kivy framework takes place in a separate language, known as the KV language. We can basically make a wireframe and put logic into it using Python. So, now we load the KV files for the widgets

7. We create some functions to fetch system parameters such as cpu usage, battery state and the current battery capacity. Note that these functions are run in separate threads using the threading module.

8. We make some custom self-updating labels using the MDLabel class

9. We create the tabs to be placed on the main screen app.

The tabs might look like they contain nothing, but the information is actually in the KV files, lets have a look!

10. We create a gridlayout to accommodate and show usage on different cores

11. Here’s what the main app code looks like

The KV file of the mainApp looks something like this

12. Finally, the execution script

13. And this is how the app should look

14: The source code for the app can be downloaded from

https://github.com/kane111/NoNonsenseCPUMonitor

Yay! You’ve made a Kivy app

credits: art_of_silverfox

What Next?

Well, now that we’ve created a good looking GUI app, what next?

  1. For the native app developer in you:
https://realpython.com/mobile-app-kivy-python/

2. For the web app developer in you:

https://pythonprogramming.net/data-visualization-application-dash-python-tutorial-introduction/

3. Always know your roots (Backend):

https://simpleisbetterthancomplex.com/series/beginners-guide/1.11/

This concludes my tutorial. Stay tuned for more!

This article would not have been possible without the invaluable contributions of people to the Python Community.

--

--