[Tutorial] Kivy App Development( Part 1)

in #utopian-io6 years ago

images.png
image source
Repository: Python, Open Source Repository

Software: For software download python 3.0 compatible with your OS here

Difficulty : Intermediate
What you will learn:
In this tutorial you will learn how to

  • This an alternative beginners guide to using the kivy module and its functions.

  • How to begin Andriod app development with kivy.

  • How fix common problems faced with using kivy.

For my previous posts in the python tutorial series
-Click Here* Creating a simple calculator using python 3.8(cpython)

Creating an encryption key(Cryptography)
- Part 1
- Part 2
- Part 3
- Part 4

Developing an institutions data collection backend and frontend with python 3.6 series
Part 1 of this series
Part 2 of this series
Part 3 of this series
Part 4 of this series
Part 5 of this series
Part 6 of this series

Previous Series
Automating OnScreen Processes
Screen Mapping and Navigation
Packaging and Deploying

Tutorial
For a beginner that hopes to start up with kivy, this tutorial would elucidate on a lot of aspects of kivy which most feel are not simplified enough for beginners. To explain the process, I would be building a Simple Tetris App from scratch and deploying it on an android platform. This tutorial would make things easier by separating development into distinguishable and comprehensive steps. Kivy is very favourable for python developers since it enables us run our programs on Andriod, ios and basically any cpu driven device. Note that kivy is specially made for Touch Screen Devices. This should get clearer within the series.

Kivy
For information on official documentation and Installation guides Click Here.

Starting Up
As stated earlier we would be building a Simple Tetris App to help understand how kivy works.
Note: I would be seperating steps for better understanding as the kivy module is quite complex and takes sometime to take in.

Importing Modules
Before you get started in kivy, it is advisable to import all neccesary modules at once at the beginning of your project. This is a listing of all the functions we will be needing and the reasons we will be needing this modules.
The first Module we would be importing is the kivy module.

 from kivy import *
#The use of this function in this code is to state the required version for kivy for this program to work
kivy.require('1.9.0')
from kivy.app import App
#This is the kivy function that lets us run our app on andriod, ios and other consoles. It is the backbone of your program
from kivy.uix.widget import Widget
#Widgets are vital for creating structures and giving commands to these structures in kivy. They come with default canvases for drawing.
from kivy.uix.label import Label
# Labels just as their name means are for adding text labels to your program.
from kivy.uix.boxlayout import BoxLayout
# Box Layouts are a set of functions that help us determine the structure of our program.

from kivy.config import Config
# This module is very important for window errors encountered during kivy startups. Without editing this module
It is common to get errors telling you to upgrade your graphic card. Using these lines of code should prevent that if it comes up
Config.set('Config','Multisample', '0')

from random import randint
kivy.clock import Clock
#This module is important in animating our widgets

Creating Our Widgets and Layouts
Apart from developing the logic for applications kivy is a package that helps us create our Logic beside our Gui.
Note: Although this module helps us do this. It is not advisable to put Gui and logic in the same file as it can mix things up alot during development and so kivy gives us a method to seperate our styling from our logic by saving the styling in a similar manner as css in the same folder as your logic file.
To explain this i would show you two instances of the same file but with different formats
main.py

#Method 1
class HelloWorldApp(App)
    def build():
        return Label(text='helloworld')

if __name__=='__main__':
    HelloWorldApp().Run()

main.py

#Method 2
class HelloWorldApp(App)
    def build():
        return Label

helloWorld.kv

<Label>:
   text: 'HelloWorld'

This two pieces of code will yield the same result but it is observable that in method1, the code is all within the main.py file while in method 2, the styling is within the helloworld.kv file while the logic is in the main.py file.
Note: The name of the .kv file must be equal to the name of the app within the main.py file and located in the same folder to enable the program to find it.
So using Method 2 Lets begin writing our game.

#First thing we Define a widget for the game
class TetrisGame(Widget):
    #Importing from the parent Widget
    pass

#Then we Define our Board or main game area
class Board(Layout):
    pass

#Then we define the main app which is the build point for program
#Fancy way of saying this is where our program runs from
class TetrisApp(App)
   def build(self):
        return TetrisGame()

if __name__=='__main__':
    TetrisApp.run()
#And standard python synthax for running our new code

So if you did everything right you should see a blank screen like the one below pop up.
Screenshot (22).png
This tutorial continues in the next of its series.

You can find the Code for this tutorial in Github

Sort:  

Thank you for your contribution.

  • In your code we suggest you make short comments with a brief description of the functions.
  • Tutorial is too short.
  • Try to improve the structure of your tutorial so that it is easy to read. I suggest you follow this template Link.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @portugalcoin!

So far this week you've reviewed 2 contributions. Keep up the good work!

Hey @yalzeee
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @yalzeee! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:
SteemitBoard and the Veterans on Steemit - The First Community Badge.

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Hi @yalzeee! We are @steem-ua, a new Steem dApp, computing UserAuthority for all accounts on Steem. We are currently in test modus upvoting quality Utopian-io contributions! Nice work!

Coin Marketplace

STEEM 0.35
TRX 0.12
JST 0.040
BTC 70753.86
ETH 3589.34
USDT 1.00
SBD 4.75