Can you obtain physical size of device in kivy?

Question:

Does anyone know how Kivy renders text in different fonts?

I have labels with text at 16sp.

On a tablets with screen sizes (1024, 552) and (2048, 1536) it works perfectly (width/height ratios 1.855 and 1.333 respectively)
enter image description here

On the pc with screen size (1280, 720) (ratio 1.778) it also displays perfectly, but on a phone with this screen size the letters are truncated
enter image description here

The only difference here is the physical size of the device. It thus appears, that Kivy text is not rendered according to some algorithm based on pixels, but takes into account the physical size of the screen

Is there any way to determine the physical size in Kivy? and hence allow me to adjust font size accordingly. The text appears correctly on the phone (small device) if I use a smaller (10sp) font size, but then it appears too small on the larger devices.

Asked By: Psionman

||

Answers:

From what I am understanding, you are having issues displaying your text properly, over multiple devices. Now, I do not think that you can get the devices actual dimensions through Kivy itself, but with plain old python, you can.

An Example in Python:

import gtk

window = gtk.Window()

screen = window.get_screen()
print "screen size: %d x %d" % (
gtk.gdk.screen_width(),gtk.gdk.screen_height())        

The Code above will print out the screen heigh and width, or resolution, for you to use.

To store them in variables instead:

import gtk

window = gtk.Window()

screen = window.get_screen()
ScreenWidth = gtk.gdk.screen_width()
ScreenHeight = gtk.gdk.screen_height()
# And to prove it worked (You would not want to include this part in code
#########################################################################
print str(ScreenWidth) + 'x' + str(ScreenHeight)

Now that you have those variables, you can use them in your .kv file, by pulling them from a python file, or implementing the whole function directly into your Kivy Code somehow.

Answered By: Moosez

From: https://kivy.org/docs/api-kivy.metrics.html we learn that ‘sp’ is the scale independent pixel size. Thus it includes the pixel density already and the font size appears the same on each device (at least should).

The reason, why your font is clipped is because of the size of the container. You also should also give the desired size, like in this example (note: if you want a secific height, you must include the size_hint_y: None argument)

TextInput:
        id: text_input
        size_hint_y: None
        height: '30sp'
        multiline: False
Answered By: DocBO

It is possible to obtain the screen sizes, but you’ll need to install plyer and before packaging also patch it, so that you could access the info.

Or just use the plain pyjnius and access android.util.DisplayMetrics with autoclass.

Answered By: Peter Badida

Yes, you can determine the physical size of screen with kivy –
You could simply use this Module:

(from kivy.core.window import Window)

and then determine sizes by writing this:

(Window.size)

Check this code out (this code determines screen physical sizes on a simple label):

⬇️⬇️

from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window

class MyApp(App):

   def build(self):

     window_sizes=Window.size

     return Label(text="screen sizes= "+str(window_sizes))

MyApp().run()
Answered By: MajdSuhail

What I ended up doing is maximizing my window to get the maximum allowed non-full screen size, then reading that size. I actually used it to center a smaller window on screen:

#-- maximize first, to get the screen size, minus any OS toolbars
Window.maximize()
maxSize = Window.system_size

#-- set the actual window size, to be slightly smaller than full screen
desiredSize = (maxSize[0]*0.9, maxSize[1]*0.9)
Window.size = desiredSize

#-- center the window
Window.left = (maxSize[0] - desiredSize[0])*0.5
Window.top = (maxSize[1] - desiredSize[1])*0.5

Note that by maximizing my window first, I am getting the maximum allowed size, less any operating system’s toolbars etc., i.e., the size that the window has when you press the maximize button.

Answered By: Elendurwen

Here’s another solution that sets the app to full screen when testing/running on Android or iOS, but uses a reasonable size (602, 1024) for testing on the desktop:

from kivy.utils import platform

class MyApp(App):
    def build(self):
        if(platform == 'android' | platform == 'ios'):
            Window.maximize()
        else:
            Window.size = (620, 1024)

        return kv

if __name__ == "__main__":
    MyApp().run()
Answered By: no-one
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.