- Kivy Tutorial
- Kivy - Home
- Kivy Basics
- Kivy - Getting Started
- Kivy - Installation
- Kivy - Architecture
- Kivy - File Syntax
- Kivy - Applications
- Kivy - Hello World
- Kivy - App Life Cycle
- Kivy - Events
- Kivy - Properties
- Kivy - Inputs
- Kivy - Behaviors
- Kivy Buttons
- Kivy - Buttons
- Kivy - Button Events
- Kivy - Button Colors
- Kivy - Button Size
- Kivy - Button Position
- Kivy - Round Buttons
- Kivy - Disabled Buttons
- Kivy - Image Button
- Kivy Widgets
- Kivy - Widgets
- Kivy - Label
- Kivy - Text Input
- Kivy - Canvas
- Kivy - Line
- Kivy - Checkbox
- Kivy - Dropdown List
- Kivy - Windows
- Kivy - ScrollView
- Kivy - Carousel
- Kivy - Slider
- Kivy - Images
- Kivy - Popup
- Kivy - Switch
- Kivy - Spinner
- Kivy - Splitter
- Kivy - Progress Bar
- Kivy - Bubble
- Kivy - Tabbed Panel
- Kivy - Scatter
- Kivy - Accordion
- Kivy - File Chooser
- Kivy - Color Picker
- Kivy - Code Input
- Kivy - Modal View
- Kivy - Toggle Button
- Kivy - Camera
- Kivy - Tree View
- Kivy - reStructuredText
- Kivy - Action Bar
- Kivy - Video Player
- Kivy - Stencil View
- Kivy - VKeyboard
- Kivy - Touch Ripple
- Kivy - Audio
- Kivy - Videos
- Kivy - Spelling
- Kivy - Effects
- Kivy - Input Recorder
- Kivy - OpenGL
- Kivy - Text
- Kivy - Text Markup
- Kivy - Settings
- Kivy Layouts
- Kivy - Layouts
- Kivy - Float Layout
- Kivy - Grid Layouts
- Kivy - Box Layouts
- Kivy - Stack Layout
- Kivy - Anchor Layout
- Kivy - Relative Layout
- Kivy - Page Layout
- Kivy - Recycle Layout
- Kivy - Layouts in Layouts
- Kivy Advanced Concepts
- Kivy - Configuration Object
- Kivy - Atlas
- Kivy - Data Loader
- Kivy - Cache Manager
- Kivy - Console
- Kivy - Animation
- Kivy - Multistroke
- Kivy - Clock
- Kivy - SVGs
- Kivy - UrlRequest
- Kivy - Clipboard
- Kivy - Factory
- Kivy - Gesture
- Kivy - Language
- Kivy - Graphics
- Kivy - Drawing
- Kivy - Packaging
- Kivy - Garden
- Kivy - Storage
- Kivy - Vector
- Kivy - Utils
- Kivy - Inspector
- Kivy - Tools
- Kivy - Logger
- Kivy - Framebuffer
- Kivy Applications and Projects
- Kivy - Drawing App
- Kivy - Calculator App
- Kivy - Stopwatch App
- Kivy - Camera Handling
- Kivy - Image Viewer
- Kivy - Bezier
- Kivy - Canvas Stress
- Kivy - Circle Drawing
- Kivy - Widget Animation
- Kivy - Miscellaneous
- Kivy Useful Resources
- Kivy - Quick Guide
- Kivy - Useful Resources
- Kivy - Discussion
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 −
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.
To Continue Learning Please Login
Login with Google