Sunday, May 25, 2014

4. Using the Kivy Language

Example 4 creates the same result, as that which was produced in Example 2, except that this time a Kivy language file is used.




A Kivy language file creates widgets, and defines their behavior. These have the extension .kv. They help separate the logic, from the presentation. However, it is possible to do everything with Python, at the expense of having more code, and harder to follow logic.




A main Kivy file is always searched. It has the same name as the App subclass, except there is no ending App in it, and everything is lowercase. Additional kv files may be loaded, with the help of the Builder class.




In the Python file, note we are importing only one user interface element, BoxLayout. We will create a class which will inherit the functionality of BoxLayout. This class is called the root class. The reason we are not importing a Button class, even though we display buttons, is that all button code has been moved to the Kivy file and thus, the Python file does not need to have the Button name, in its namespace.




This is the root class, which inherits from the BoxLayout class. It has two functions, which will be called from the Kivy file, after a button is clicked. Note, the word return is not necessary. You might put blank lines, instead, of return, to make its meaning clear.




The Application class just returns an instance of the Root class.




The main code is same as before. If you run the program, you can see that the App name is helloworld, all in lowercase.


# helloworld.py

from kivy.uix.boxlayout import BoxLayout
from kivy.app import App

import time

class MyBox(BoxLayout):
    def hello(self, *args):
        print("--> Hello at time %s" % time.ctime())
        return
    def world(self, *args):
        print("--> World! at time %s" % time.ctime())
        return
   
class HelloWorldApp(App):
    def build(self):
        return MyBox()

if __name__=='__main__':
    myApp = HelloWorldApp()
    print("The name of your app is %s" % myApp.name)
    myApp.run()




The Kivy file defines a rule for our root class. All rules are in the angle brackets. Since MyBox class implements the functionality of BoxLayout, the Buttons are arranged horizontally, the default orientation. Buttons have different properties. We define 2 here, the text and on_press. The text indicates what is displayed. The on_press is called whenever a click occurs, on the particular widget. The right-hand side calls either the hello() or the world() function of the root class. In fact, the right-hand side could be any Python code.


# helloworld.kv

<MyBox>:
    Button:
        text: 'Hello'
        on_press: root.hello(*args)
    Button:
        text: 'World!'
        on_press: root.world(*args)





1 comment:

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    Python Training in electronic city

    ReplyDelete