14

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Psionman
  • 3,084
  • 1
  • 32
  • 65

6 Answers6

11

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()
J. Murray
  • 1,460
  • 11
  • 19
MajdSuhail
  • 184
  • 2
  • 5
  • Window.size is not physical size of device. It is a size of actual window. I also looking for same ... – Nikola Lukic Feb 21 '21 at 15:53
  • Window.size just returns the default size of a Window... unless you've already set the size at which point it returns whatever you set it to. – no-one Mar 15 '23 at 13:20
1

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.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
1

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.

Lenka Pitonakova
  • 979
  • 12
  • 14
0

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.

Moosez
  • 68
  • 4
  • Hi Moosez. You have understood my problem precisely. However, it doesn't seem to be that simple to get gtk and kivy to work together. It works fine in an emulator, but when I try to use it on a device it crashes :( Sorry. Is gtk 'plain old python'? – Psionman Jul 26 '16 at 08:13
  • 2
    GTK is a completely diffferent toolkit, and I don't think it even supports android. – inclement Dec 15 '18 at 17:05
0

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
DocBO
  • 7
  • 1
0

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()
no-one
  • 141
  • 6