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.
To make the Login page, we first have to import some features of kivy – widgets, gridlayout.
# base Class of your App inherits from the App class. from kivy.app import App # GridLayout arranges children in a matrix. from kivy.uix.gridlayout import GridLayout # Label is used to label something from kivy.uix.label import Label # used to take input from users from kivy.uix.textinput import TextInput class LoginScreen(GridLayout): def __init__(self, **var_args): super(LoginScreen, self).__init__(**var_args) # super function can be used to gain access # to inherited methods from a parent or sibling class # that has been overwritten in a class object. self.cols = 2 # You can change it accordingly self.add_widget(Label(text ='User Name')) self.username = TextInput(multiline = True) # multiline is used to take # multiline input if it is true self.add_widget(self.username) self.add_widget(Label(text ='password')) self.password = TextInput(password = True, multiline = False) # password true is used to hide it # by * self.add_widget(self.password) self.add_widget(Label(text ='Comfirm password')) self.password = TextInput(password = True, multiline = False) self.add_widget(self.password) # the Base Class of our Kivy App class MyApp(App): def build(self): # return a LoginScreen() as a root widget return LoginScreen() if __name__ == '__main__': MyApp().run() |
Output:

Code Explanation:
class LoginScreen(GridLayout) :
- In this class, we made the grids and the blocks like username, password and provide the fuctionality 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.def__init__(self,**kwargs):super(LoginScreen,self).__init__(**kwargs)chevron_rightfilter_noneOne 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**kwargswhile 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.self.cols=2self.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)chevron_rightfilter_none
class MyApp :
- This class is derived from the
App() classof the kivy.app. This class is the base class for creaking the kivy Application. It is bassically the main entery 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 specificApp() class, when we are ready to start, we call the instanceApp().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 therun()method Launches the app in standalone mode and calls theclass MyAppwhich returns theLoginscreen() 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 :


Recommended Posts:
- Python | Add Label to a kivy window
- Python | Window size Adjustment in Kivy
- Python| AnchorLayout in Kivy
- Python | StackLayout in Kivy
- Python | Animation in Kivy
- GridLayouts in Kivy | Python
- Python | Kivy .kv File
- Python | Canvas in kivy
- Python | PageLayout in Kivy
- Python | Float Layout in Kivy
- Python | Ellipse (different polygons) in Kivy
- Python | PageLayout in Kivy using .kv file
- Python | Line (Canvas) in kivy
- Python | Spinner widget in kivy
- Python | Switch widget in Kivy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet



