Application in Kivy for different devices


Keywords:size 


Question: 

As premise: I'm a beginner in the world of programming.

I would like to create my first application which should run on different devices, i.e. Smartphones or tablets with different screen sizes.

The main window should adapt automatically to the height of the device. I found on this forum two ways to control window size.

With the first one, I can determine the window size:

from kivy.config import Config
Config.set('graphics', 'width',  200)
Config.set('graphics', 'height', 100)

But this is a static method with constant values. Alternatively, I could set the window to full screen:

from kivy.config import Config
Config.set('graphics', 'fullscreen', 1)

This way the window covers the whole display and changes the proportion of the window and of the widgets inside…

Is there a way to get the display height and use it to build my window?


1 Answer: 

One way is to check the platform and try getting the dimension based on the platform. for example on linux you could run xdpyinfo | grep dimensions to get the screen resolution or on windows from win32api import GetSystemMetrics is used for getting the resolution or on android you could us pyjnius to Get screen dimensions in pixels

as an example, for linux you could

import subprocess

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.utils import platform

class Main(Widget):
    def __init__(self):
        super(Main, self).__init__()
        if platform == 'linux':
            command = "xdpyinfo  | grep -oP 'dimensions:\s+\K\S+'"
            ps = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            output = ps.communicate()[0]
            self.add_widget(Label(text=output))


class MyApp(App):
    def build(self):
        return Main()

MyApp().run()