Kivy – Applications


Kivy – Applications



”;


An application written with the Kivy framework is represented by an object of a class that inherits the “kivy.app.App” class. Calling the run() method of this object starts the application, and enters in an infinite event loop.

Application GUI is set up either by overriding the build() method in App class, or by providing a corresponding “.kv” file.

Application Configuration

If you want to provide a customized configuration of one or more parameters, a config.ini file will be created when build_config() method of the App class is invoked.

Here is an example of build_config() method. It stores values for two parameters in “section1” of the “ini” file. The name of “ini” file will be the same as the app class (without “App” suffix if it has). So, if your app class is “HelloApp”, then the “ini” file will be created as “hello.ini”. The parameters from this file will be loaded when the build() method is invoked.


def build_config(self, config):
   config.setdefaults(''section1'', {
      ''Company'': ''TutorialsPoint'',
      ''year'': ''2023''
   })

As soon as a section is added, the “hello.ini” file will be created in the same directory which contains the “hello.py” file.

Load and use the configuration settings in the build() method as follows −


def build(self):
   config = self.config
   l1 = Label(text="© {} Year {}".format(
      config.get(''section1'', ''company''),
      config.getint(''section1'', ''year'')),
      font_size=40)
   return l1

When the application is run, the Label will be populated by reading the “config” file.

Example

Here is the complete program −


from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window

class HelloApp(App):
   Window.size = (720, 300)

   def build_config(self, config):
      config.setdefaults(''section1'', {
         ''Company'': ''TutorialsPoint'',
         ''year'': ''2023''
      })

   def build(self):
      config = self.config
      l1 = Label(text="© {} Year {}".format(
         config.get(''section1'', ''company''),
         config.getint(''section1'', ''year'')),
         font_size=40)
      return l1

app = HelloApp()
app.run()

Output

When you run the application, it will produce the following window as the output


Kivy Applications

Look for the “hello.ini” file created in the application folder. When opened with a text editor, it shows the following contents −


[section1]
company = TutorialsPoint
year = 2023

Instance Methods in the App Class

The App class defines the following instance methods −

  • build() − This method initializes the application and is called only once. If this method returns a widget (tree), it will be used as the root widget and added to the window.

  • build_config() − This method constructs ConfigParser object before the application is initialized. Based on any default section / key / value for your config that you put here, the “ini” file will be created in the local directory.

  • load_config() − This function returns a ConfigParser with the application configuration.

  • load_kv() − This method is invoked the first time the app is being run if no widget tree has been constructed before for this app. This method then looks for a matching the “kv” file in the same directory as file that contains the application class.

  • pause() − This method causes the application to be paused.

  • run() − When called, this method launches the app in standalone mode.

  • stop() −This method stops the application.

  • on_pause() − This is an event handler method that is called when Pause mode is requested. If it returns True, the app can go into Pause mode, otherwise the application will be stopped.

  • on_resume() − An event handler method which resumes the application from the Pause mode.

  • on_start() − This method is the event handler for the ”on_start” event. It is fired after initialization (after build() has been called) but before the application has started running.

  • on_stop() − The ”on_stop” event which is fired when the application has finished running (i.e., the window is about to be closed). This method handles on_stop event.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *