I assume you've read my previous post, "Is Python a language for creating GUIs?" If so, I invite you to create a label using Python and Kivy. In my previous post, I demonstrated how to install Kivy in the PyCharm IDE, and we shall start from there. However, it is not necessary to use PyCharm to create a label; it can also be done using the command line if you have Kivy and Python installed on your computer. PyCharm is particularly useful if you plan to create multiple projects and more involved programs.
Why a Label?
GUIs comprise many kinds of widgets, and perhaps the label is one of the most common and obvious elements. Labels can be seen individually or as part of larger widgets with smaller sub-widgets, such as in a form or an authentication GUI.
In its most basic form, a label is a small object that usually carries a piece of text, such as 'Welcome' or 'Hello World.' It may even carry a small image.
How do you create a label using Python?
As mentioned earlier you need Python to create a label but need a library such as Kivy. It is assumed that you have both Python and Kivy installed on your computer/laptop. Since, PyCharm is used in this example as the IDE, you create a Python file in a project in PyCharm.
Python is a declarative language and you create upfront your python file, LabelBasic.py shown here:
===============
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text='This is a basic label')
if __name__ == '__main__':
MyApp().run()
These are basic things happenings:
1. You import necessary modules:
import kivy: imports Kivy library which provides tools for cross-platform GUIS.
import kivy.app import App: imports the App Class for creating kivy applications.
import kivy.uix.label import label: imports label Class to display text or images on the GUI.
2. class MyApp(App): Creates a class named MyApp that inherits from App class represent kivy application.
def build(self): This method is required in every kivy app. It is called when the application starts and is responsible for building the user interface.
return label(text='This is a basic label'): This creates an instance of the label class and sets its text property to 'This is a basic label'. This Label instance is then returned as the root widget of the application's UI.
3. Running the application:
if __name__=='__main__': ensures that the following code is executed only when the script is run directly.
MyApp().run(): creates an instance of the MyApp class and calls its run() method. This starts the Kivy application and displays the GUI.
This is the standard way of creating a GUI using Python code and the Kivy library. The PyCharm IDE has been used because all the previous posts were written to demonstrate many other types of applications such as Image studies. It is not necessary for creating a GUI element. It can be run if you have Python installed using the command-line.
The basic premise behind this code is creating an object class and instantiating the class and calling its properties to create a kivy widget.
Result of running the code:
The exception thrown running this code in PyCharm is shown here:
================
C:\Users\hoden\AppData\Local\Programs\Python\Python312\python.exe C:\Users\hoden\PycharmProjects\PyWidgets\LabelBasic.py
[INFO ] [Logger ] Record log in C:\Users\hoden\.kivy\logs\kivy_25-01-12_11.txt
[INFO ] [deps ] Successfully imported "kivy_deps.angle" 0.4.0
[INFO ] [deps ] Successfully imported "kivy_deps.glew" 0.3.1
[INFO ] [deps ] Successfully imported "kivy_deps.sdl2" 0.8.0
[INFO ] [Kivy ] v2.3.1
[INFO ] [Kivy ] Installed at "C:\Users\hoden\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\__init__.py"
[INFO ] [Python ] v3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]
[INFO ] [Python ] Interpreter at "C:\Users\hoden\AppData\Local\Programs\Python\Python312\python.exe"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 195 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[CRITICAL] App.root must be an _instance_ of Widget
Traceback (most recent call last):
File "C:\Users\hoden\PycharmProjects\PyWidgets\LabelBasic.py", line 13, in <module>
MyApp().run()
File "C:\Users\hoden\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\app.py", line 955, in run
self._run_prepare()
File "C:\Users\hoden\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\app.py", line 931, in _run_prepare
raise Exception('Invalid instance in App.root')
Exception: Invalid instance in App.root
Process finished with exit code 1
================
There have been many instances where trying to run this simple code has resulted in the same kind of problem with the exception that ' Invalid instance in App.root. Process finished with exit code 1.
Here are the attempts:
2. Here are few other examples of code failure:
What is the solution?
I have been looking at this problem and trying to diagnose and finally arrived at a work around. I basically suspect that the Run() method has some basic problem and I have been able to work around to make the display the label. My workaround needs introducing a Python decorator '@property' after the class statement but before the widget's build, def build(self).
The Modified code that runs error free:
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
@property
def build(self):
return Label(text='This is a basic label')
if __name__ == '__main__':
MyApp().run()
When this is run, the Process finished with exit code 0 and you see the following display.
This is indeed a successful run as the label is displayed. I have gone back to Stack Overflow and made some of the cold files to work with this fix.