Kivy - Miscellaneous



Kivy - Exception Handling

In programming, Exception handling refers to the mechanism to prevent the program from crashing if it encounters run-time errors. Kivy provides a class called ExceptionHandler that can manage exceptions raised by Kivy or by your own code.

The ExceptionManager class is defined in the "kivy.base" module. You need to import it from "kivy.base" and access the instance that handles Kivy exceptions. You can use this class to add custom handlers for different types of exceptions, or to override the default behavior of Kivy when an exception occurs. For example, you can use the handle_exception method to log the exception, show a message to the user, or exit the app gracefully.

from kivy.base import ExceptionHandler, ExceptionManager
from logging import Logger

class handler(ExceptionHandler):
   def handle_exception(self, inst):
      Logger.exception('Exception caught by ExceptionHandler')
      return ExceptionManager.PASS
      
ExceptionManager.add_handler(handler())

A handler function that takes the exception as an argument and returns one of the following values −

  • ExceptionManager.PASS − The exception should be ignored as it was handled by the handler.

  • ExceptionManager.RAISE − The exception should be re-raised.

  • ExceptionManager.USER_HANDLED − The exception was handled by the user and should not be logged.

You can also use the handle_exception method to manually handle an exception using the registered handlers.

Kivy - Resources Management

The "kivy.resources" module includes the functionality to for searching for specific resources across a list of paths particularly if you application deals with multiple paths and projects.

When Kivy looks for any resource such as an image file, or a "kv" file, it searches through a predetermined set of folders. You can modify this folder list using the resource_add_path() and resource_remove_path() functions.

If you want to use any alternative for the default style.kv or data/defaulttheme0.png you can add the path to your preferred alternatives via the resource_add_path() method.

Following functions are defined in the "kivy.resources" module −

  • resource_add_path(path) − Add a custom path to search in.

  • resource_find(filename, use_cache=False) − Search for a resource in the list of paths. Find results are cached for 60 seconds. This can be disabled using use_cache=False.

  • resource_remove_path(path) − Remove a search path.

Kivy - Weak Proxy

Python uses reference counting algorithm for garbage collection, by keeping count of how many objects are referencing a certain object. If the garbage collector finds that an object is referenced by another object, it can't be garbage collected. If the counter reaches zero, the garbage collector will free that object.

A weak reference is a reference that does not protect the object from getting garbage collected. To create weak references, Python has provided us with a module named weakref.

Kivy defines WeakProxy class in kivy.weakproxy module. In order to allow garbage collection, the WeakProxy class provides weak references to objects. It effectively enhances the weakref.proxy by adding comparison support.

Kivy - Context

The Kivy objects Clock, Cache and Builder are global objects. To use them in the context of the present application you will have to register it. The kivy.context module defines a Context class, which inherits the properties of Python's built-in dict class.

In addition to the dict methods, we have the following functions defined in this module −

  • get_current_context() − Return the current context.

  • egister_context(name, cls, *args, **kwargs) − Register a new context.

Advertisements