Window Size Adjustment
Kivy is a cross-platform toolkit in Python to develop applications. So, the size does not matter as it is self-adjusting. But, sometimes, we may need to fix the size of the window as per requirements. We can fix the size either height-wise or width-wise.
Luckily, Kivy provides the kivy.config module to adjust the size of the window. For resizing the window size of an application in kivy, we import the following libraries and modules in python:
from kivy.config import Config

You can also try this code with Online Python Compiler
Run Code
Kivy Config File
A configuration file determines the default settings of Kivy. You can manually edit this file or use the Config object to adjust these values. We can use configuration objects for a variety of other parameters, but that's outside the focus of this post, so let's stick to the window size for now.
We must import the Config class to set the window size. Then we have to configure the window's width and height. To accomplish this, we use the Config object's set method.
Syntax:
Config.set('graphics', 'width', '800')

You can also try this code with Online Python Compiler
Run Code
There are three parameters to consider:
- The configuration token to which our option belongs.
- The choice, here, is width or height for window size.
- The value—this is the value we want our option to be set to.
Fixed Window
In the below example, we have implemented a program to set the fixed window size of the application to a height of 400px and a width of 800px.
Code
#program for window size in kivy
from kivy.config import Config
#set window size
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '400')
#import kivy toolkit
import kivy
#required for kivy
kivy.require('1.0.6')
#import kivy classes
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
#create a class for the app
class MyApp(App):
def build(self):
return Label(text='Hello world')
#create the object of the class and run the app
obj = MyApp()
obj.run()

You can also try this code with Online Python Compiler
Run Code
Explanation
In the program, we have imported the kivy.config module, which allows us to set the window size of an application. Then we set the window size using the Config.set method. Then we imported various Kivy classes required for the application. Then we have created the class and its objects. In the end, we have called the object using obj.run().
Output

We can notice the size of the window's height and width, which were set to ‘400’ and ‘800’ respectively.
Resizable Window
The other way to perform window sizing adjustment is to give the user freedom to resize the window. In the below example, we will see how a user can resize the window by themselves. We don’t need to specify the width and height of the window.
Code
#program for resizable window in kivy
import kivy
kivy.require('1.0.6')
from kivy.config import Config
#set the window size to be resizable
Config.set('graphics', 'resizable', True)
#import kivy classes
from kivy.app import App
from kivy.uix.label import Label
#create a class for the app
class MyApp(App):
def build(self):
return Label(text='Hello World!')
#run the app
if __name__ == '__main__':
MyApp().run()

You can also try this code with Online Python Compiler
Run Code
Explanation
Previously, in the “Config.set” property, we set the width and height of the window from our end. But, here we have set the resizable property value as “True” that makes the window resizable as can be seen in the above code.
Output

In the output window, we can see that a cursor is present at the corner that can be used to resize the active window.
Frequently Asked Questions
Which is better: Kivy or PyQt5?
There is little difference between Kivy and PyQt when working with GUI in Python, but they are both frameworks that work on different applications. For example, Kivy is much better for mobile and multi-touch apps than desktop apps, whereas PyQt is much better for desktop apps.
Is learning Kivy difficult?
Kivy is a simple language to learn and use, although it is still in its early stages of development, with few people working in this field. Help is available on various internet platforms, but it can be challenging to find solutions that are tailored to your problem.
Is Python suitable for mobile applications?
Python is a free and open-source programming language that we can use to create online and mobile applications. Python is used to create apps like Instagram and Dropbox.
Is it possible to create Android apps using Python?
Even though Android does not enable native Python programming, Python can be used to create Android apps. We can use various technologies and toolkits of Python to convert Python apps into Android packages that can be executed on Android devices.
Conclusion
In this article, we have discussed Kivy library basics and how to resize an application window. We learnt different features of Kivy and how to adjust the window of the Kivy application and then implement it using Python.
We hope that this blog has helped you enhance your knowledge on how to adjust window size in kivy using python. If you would like to learn more, check out our articles on the ‘Button colour in kivy,’ ‘Scroll View Widget in Kivy,’ ‘Button action in kivy,’ ‘Dropdown list in Kivy,’ ‘Kivy float layout.’ Do upvote our blog to help other ninjas grow.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundles, follow guided paths for placement preparations and much more!
Happy Reading!