Python | Make a simple window using kivy
Kivy is a platform independent as it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it Does not mean that it can not be used on Desktops applications.
Use this command To install kivy:
pip install kivy
Now in this article, we will discuss how to build a window in kivy, just like a login screen which is able to take the user name and password from the user without the functionality just the layout of this.
Kivy Tutorial – Learn Kivy with Examples.
To make the Login page, we first have to import some features of kivy – widgets, gridlayout.
Python3
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class LoginScreen(GridLayout):
def __init__( self , * * var_args):
super (LoginScreen, self ).__init__( * * var_args)
self .cols = 2
self .add_widget(Label(text = 'User Name' ))
self .username = TextInput(multiline = True )
self .add_widget( self .username)
self .add_widget(Label(text = 'password' ))
self .password = TextInput(password = True , multiline = False )
self .add_widget(Label(text = 'Comfirm password' ))
self .password = TextInput(password = True , multiline = False )
self .add_widget( self .password)
class MyApp(App):
def build( self ):
return LoginScreen()
if __name__ = = '__main__' :
MyApp().run()
|
Code Explanation:
- The code starts by declaring a class called LoginScreen.
- This class inherits from the App class which is used to create an instance of it.
- The __init__ function is then defined in the constructor of this class and takes two arguments: **var_args, which are passed on to super() when instantiating the object, and self.cols, which defines how many columns there will be in our grid layout.
- The next line creates a Label widget that has text set as “User Name” and adds it to our login screen’s matrix using add_widget().
- Next, we create a TextInput widget with multiline=True so that users can type their username into it multiple times if they want to do so.
- After creating these widgets we call add_widget() again but pass self.username instead of the label because labels are not allowed inside other widgets like TextInputs or Buttons (which would otherwise be placed inside them).
- We also change multiline=True for password=True since passwords cannot be typed more than once per input field (password true hides them).
- After adding these widgets we have created all the necessary components for our login screen!
- The code is used to create a class called MyApp that inherits from the App class.
- The LoginScreen() function is then created and returned as the root widget of the MyApp.
Output:
Code Explanation:
class LoginScreen(GridLayout): In this class, we made the grids and the blocks like username, and password and provide the functionality of text input. Let’s see the detailed description now. In the class LoginScreen, we override the method __init__() so as to add widgets and to define their behavior.
Python3
def __init__( self , * * kwargs):
super (LoginScreen, self ).__init__( * * kwargs)
|
self.add_widget(Label(text='password'))
self.password = TextInput(password=True, multiline=False)
self.add_widget(self.password)
One should not forget to call super in order to implement the functionality of the original class being
overloaded. Also note that it is good practice not to omit the **kwargs while calling super, as they are sometimes used internally.
We ask the GridLayout to manage its children in two columns and add a Label and a TextInput for
the username and password.
Python
self .cols = 2
self .add_widget(Label(text = 'User Name' ))
self .username = TextInput(multiline = False )
self .add_widget( self .username)
self .add_widget(Label(text = 'password' ))
self .password = TextInput(password = True , multiline = False )
self .add_widget( self .password)
|
class MyApp :
- This class is derived from the App() class of the kivy.app. This class is the base class for creaking the kivy Application. It is basically the main entry point into the kivy run loop.
In most of the cases, we subclass this class and makes our own App. we create the instance of the specific App() class, when we are ready to start, we call the instance App().run() method.
Here the build() method “Initializes the application; it will be called only once. If this method returns a widget (tree), it will be used as the root widget and added to the window.
Returns:None or a root Widget instance if no self.root exists.”
- MyApp.run() is the run() method Launches the app in standalone mode and calls the class MyApp which returns the Loginscreen() class.
The best part is that Try re-sizing the window and you will see that the widgets on screen adjust themselves according to the size of the window because widgets use size hinting(adjustment) by default.
For example :
Last Updated :
29 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...