Buttons and Labels
There are a few conventions we need to follow when creating a .kv file.
Naming: The name of your .kv file must follow the rules below in order for python/kivy to be able to see and load the file.
- It must be all lowercase
- It must match with the name of your main class. (The one that has the build method)
- If the name of your main class ends in “app” (lowercase or uppercase) you must not include “app” in your file name.
File Location: The file must be in the same directory as your python script.
File Extension: The file must be saved as type *All Files and end with .kv
For example:
My main classes name is MyApp. Therefore I will name my file my.kv.
|
|
Before continuing please import the following module from your python script.
from kivy.uix.widget import Widget
The first thing we need to do to add widgets to our screen using .kv is set up an empty class in our python script. This class will simply inherit from Widget and will be what is used from within the kv file to add widgets.
class MyGrid(Widget):
pass
class MyApp(App):
def build(self):
return MyGrid()
Two important things to remember about .kv files:
- Capitals are Important
- Indentation is important
The first thing we do when writing in a .kv is declare the class we will be adding widgets to. In my case it’s MyGrid.
# Filename: my.kv
<MyGrid>:
# Filename: my.kv
<MyGrid>:
Label:
text: "Name: "
TextInput:
multiline:False
The file below is a form asking the user for their name and email address using a kv file. You can see that we have multiple grid layouts and that we add widgets to those layouts.
Notice that to make these widgets fill the entire screen we must change the size attribute. By making the size of the first grid layout root.width, root.height we are telling it to fill the size of the window dynamically.
# Filename: my.kv
<MyGrid>:
GridLayout:
cols:1
size: root.width, root.height
GridLayout:
cols:2
Label:
text: "Name: "
TextInput:
multinline:False
Label:
text: "Email: "
TextInput:
multiline:False
Button:
text:"Submit"
on_press: root.btn()
After experimenting with the .kv language some of you may have asked the question: How do we access our elements (textinput, button etc.) from our python script? Well that is an excellent question and is what we have object properties for!
An object property allows us to create a reference to widgets from within a .kv file from our python script.
To set up object properties we need to create global variables from within our .kv file and assign these variables to the id property of specific widgets.
<MyGrid>:
name: name # Global variable name references the id name
email: email # Global variable email references the id email
GridLayout:
cols:1
size: root.width - 200, root.height -200
pos: 100, 100
GridLayout:
cols:2
Label:
text: "Name: "
TextInput:
id: name # <- Add this
multiline:False
Label:
text: "Email: "
TextInput:
id: email # <-Add this
multiline:False
Button:
text:"Submit"
The first thing we need to do to use an object property from within our python script is import the specific module.
from kivy.properties import ObjectProperty
class MyGrid(Widget):
name = ObjectProperty(None)
email = ObjectProperty(None)
Like we had in previous tutorials we’d like our button to perform a function when it is clicked. To accomplish this we need to create a method inside of our MyGrid class that we can call each time our button is pressed. We will call this btn().
# Goes inside MyGrid Class
def btn(self):
print("Name:", self.name.text, "email:", self.email.text)
self.name.text = ""
self.email.text = ""
Now we need to tell the button what it should call when it is pressed. To do this we will add the property on_press in our .kv file.
Button:
text:"Submit"
on_press: root.btn() # <- Add this
Now we should have a functioning application that works.
|
|
<MyGrid>:
name: name
email: email
GridLayout:
cols:1
size: root.width - 200, root.height -200
pos: 100, 100
GridLayout:
cols:2
Label:
text: "Name: "
TextInput:
id: name
multiline:False
Label:
text: "Email: "
TextInput:
id: email
multiline:False
Button:
text:"Submit"
on_press: root.btn()