Showing posts with label KIVY. Show all posts
Showing posts with label KIVY. Show all posts

Sunday, January 12, 2025

Are you ready creating a label using Python?

 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:

1.https://stackoverflow.com/questions/70997766/why-do-i-get-the-error-invalid-instance-in-app-root-in-kivy-python/79331939#79331939

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.



Saturday, January 11, 2025

Is Python a language for GUI development?

 

The introduction of Computer applications with a graphic user interface was significant milestone in moving Computers and their utility from specialists and technologists to the general public. This was significantly impacted by the 'Desktop' revolution that shifted requirements from Main Frame to Tabletop devices. This also shifted the computer applications from procedural languages to more 'Object' based or object oriented languages and ushered the Internet revolution and presently enjoys enormous popularity because of their utility in every aspect of life.

GUIs simplified interactions with computers, making them more intuitive and user-friendly and led to the democratization of computer access. GUIs in Internet based applications propelled their utility in World Wide Web, e-commerce, social media and online communications and learning.

In the early days, GUIs were exclusively built using low-level languages such as C and C++. With the introduction of higher-level languages like Visual Basic and Java, object creation became significantly easier. The development of the Document Object Model (DOM) further facilitated the integration of GUIs into the Internet through web-based languages like JavaScript.

Python, being an interpreted language, doesn't come with built-in GUI elements. However, it boasts a rich ecosystem of libraries that enable the creation of sophisticated GUIs. Libraries such as Tkinter, PyQt, and Kivy offer powerful tools for developing graphical interfaces in Python.

For example:

Tkinter: A standard Python interface to the Tk GUI toolkit, great for simple applications.[https://wiki.python.org/moin/TkInter]

PyQt: A set of Python bindings for the Qt application framework, suitable for more complex and feature-rich applications.[https://en.wikipedia.org/wiki/PyQt]

Kivy: An open-source Python library for developing multitouch applications, excellent for mobile and cross-platform apps.[https://kivy.org/]

By leveraging these libraries, developers can create GUIs with superior qualities, making Python a versatile choice for both backend and frontend development. Such integration would require connection to databases and web interactions.

These libraries have means for making such a thing possible.

  • Tkinter can connect to SQLite and interact with web making requests and urllib
  • PyQt has more advanced option supporting network operations like PyQt Network and SQLAlchemy for database interactions. 
  • Kivy also uses SQLAlchemy or peewee for database interactions and requests for web interaction
For starters we will be looking at Kivy in my future posts.




Sunday, January 5, 2025

As an App developer have you tried python library KIVY? Do you know how to install it on PyCharm?

 

There are many platforms for writing Apps for mobile phones and KIVY is a open-source Python library. It can be used for developing multitouch apps on mobile devices. It's strength is once created it can run on various platforms including Windows, macOS, Linux, Android and iOS. 

Key Features of Kivy:

  • Cross-Platform: Kivy allows you to write your code once and run it on multiple platforms without modification. This is incredibly useful for developers who want to reach a broad audience.
  • Multitouch Support: Kivy is designed with multitouch in mind, making it ideal for applications that require touch input, such as mobile apps and interactive kiosks.
  • Customizable Widgets: Kivy provides a wide range of customizable widgets, allowing developers to create unique and visually appealing user interfaces. Note: It has no Radio Button.
  • GPU Acceleration: Kivy uses OpenGL to provide GPU-accelerated graphics, ensuring smooth and responsive user interfaces.
  • Community and Documentation: Kivy has a strong community and extensive documentation, making it easier for developers to find support and resources.

Is Python for GUI devleopment?

It is versatile, easy to design with straight forward syntax and run smoothly and responsively. It has the capability for multitouch and is made for 'Mobile' apps.

This post shows you how to import the KIVY library into PyCharm. PyCharm has native support for KIVY and getting it into PyCharm is a piece of cake!


Why PyCharm? 

It is the platform I am using now to develop some base knowledge for AI, Image Processing, etc. One could use Visual Basic Community Edition.

Recently, I have published many posts on PyCharm in this blog. Search for PyCharm on this blog http://hodentekHelp.blogspot.com

Importing KIVY into PyCharm:

It is very easy as the library is already in PyCharm. However, for your project you need to install it before importing into Python code.

I created a project, PyWidgets and then searched here for KIVY as shown.


Just click on Install and then after installation click on Add Package. The library package will be added to the project. You can read and contact KIVY groups from this screen.


KIVY as stated earlier is not a standalone program but a library. To use this, you need a Python program into which you bring in this library.

In future posts the usage of this library will be described and discussed. It is an important part of multi-touch mobile app development.