Introduction
Kivy is a Python platform-independent GUI tool. Because it can run on Android, iOS, Linux, and Windows, among other platforms, it is mainly used to construct Android applications, but it may also be used to develop desktop programs.
We first need to install the Kivy library to use the background template. Type the following command to install Kivy.
pip install kivy
In this blog, we learn about the Background Template in Kivy. Let us see how to apply the background in our apps using kivy library.
Background Template
A good background makes your App look attractive. To insert background in our App, we have to make a .kv file. Below is the code for setting the background in our App. Let us take a look and try to understand.
We will write the following code in main.py.We will type python main.py in our command prompt to run the python file.
# Program to apply a background template for the App
# Import necessary modules from kivy
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
# Create a background class that inherits the box layout class
class Background(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
pass
# Create an App class with the name of your App
class SampleApp(App):
# Return the Window having the background template.
def build(self):
return Background()
# Run app in the main function
if __name__ == '__main__':
SampleApp().run()
We will write the following code in .kv file.
<Background>:
id: main_win
orientation: "vertical"
spacing: 10
space_x: self.size[0]/3
canvas.before:
Color:
rgba: (1, 1, 1, 1)
Rectangle:
source:'back.jfif'
size: root.width, root.height
pos: self.pos
Button:
text: "Click Me"
pos_hint :{'center_x':0.2, 'center_y':0.2}
size_hint: .30, 0
background_color: (0.06, .36, .4, .675)
font_size: 40
Output
Now, let us move to the FAQs section to clear our doubts regarding the background template in kivy.