pythonpython-3.xkivy

Changing the size and position of widgets according to window size in Kivy


I've been recently trying to use the library Kivy to make applications that will go onto different devices. Each app is known to resize their elements on-screen according to window size and elements having the same definitions. Since I'm pretty new to the world of Kivy, I was wondering how such a thing can be done.

For example:

This is a resolution of 1000x750 enter image description here

and this is the resolution of 500x350

enter image description here

Notice how the text resizes with the image.

Anything would be greatly appreciated, thanks!


Solution

  • In kivy most of widgets have an argument called size_hint which as the name suggests sets size of widget according to screen size. It takes value from 0 to 1. A value of 0.5 basically means half of screen. You can use size_hint_x and size_hint_y to define size along x and y axis or together you can use size_hint = (0.5,0.5). Also, if in some cases when you couldn't use size_hint then you can you Window.size to get the screen size of the device as a tuple. Assume you want to make something with size 50% of both x and y. Then you can set size = (Window.size[0]*0.5, Windows.size[1]*0.5) Window.size[0] is basically length along x-axis and Window.size[1] along y axis