Metrics.dp < 0?

71 views
Skip to first unread message

Mike

unread,
Mar 10, 2025, 11:51:29 AMMar 10
to Kivy users support
Android Debug APK with buildozer, Ubuntu 24.04 with Python 3.12.3, Kivy 2.3.0 and 2.3.1, Android 14:

Metrics.dp = -1.000000 (!), Metrics.sp = 1.00000

For example, a "width: dp(1.01)" in Kivy kv (Builder.load_string) results in a "width = 0" runtime error. No Problem with Kivy 1.1.1 and Python 2, same mobile phone. If I take a fixed value without dp(), I no longer get a runtime error with the APK. But - probably because I have a lot of calculations in the kv with dp() the app is as small as a postage stamp one the mobile phone (for example metrics.dp(1.01) = -1.010000) and the view is broken.

Windows with Python 3.13.1 and Kivy 2.3.1:

Metrics.dp = 1.000000, Metrics.sp = 1.00000

No error and the display is correct.

Does anyone have an idea what is happening and how to fix it?

I can replace dp() with sp(), but that only fixes some of the symptoms, not the root cause.

Mike

unread,
Mar 10, 2025, 4:39:11 PMMar 10
to Kivy users support
Now it gets strange. The information from my post came from some log outputs directly after some imports.

<kv loaded: inline code, kv as string and loading with Builder.load_string()>

from kivy.metrics import dp,sp
log.info('TEST: Metrics.dp = %f' % (Metrics.dp))
log.info('TEST: metrics.dp(1.01) = %f' % (dp(1.01)))
log.info('TEST: Metrics.sp = %f' % (Metrics.sp))

03-10 17:05:29.968 19755 19793 I python  : [INFO   ] [Text        ] Provider: sdl2
03-10 17:05:29.973 19755 19793 I python  : [INFO   ] [Audio       ] Providers: audio_sdl2 (audio_android, audio_ffpyplayer ignored)
03-10 17:05:29.995 19755 19793 I python  : [INFO   ] [TEST        ] device screen width 6.1 cm, HANDY detected
03-10 17:05:30.012 19755 19793 I python  : [INFO   ] [TEST        ] Metrics.dp = -1.000000
03-10 17:05:30.012 19755 19793 I python  : [INFO   ] [TEST        ] metrics.dp(1.01) = -1.010000
03-10 17:05:30.012 19755 19793 I python  : [INFO   ] [TEST        ] Metrics.sp = 1.000000


(copied from below, therefore same times)

Then I made a few more log outputs:

from kivy.metrics import dp,sp
log.info('TEST: Metrics.dp = %f' % (Metrics.dp))
log.info('TEST: metrics.dp(1.01) = %f' % (dp(1.01)))
log.info('TEST: Metrics.sp = %f' % (Metrics.sp))
#log.info('TEST: metrics.sp(1.01) = %f' % (sp(1.01)))
log.info('TEST: Metrics.dpi = %f' % (Metrics.dpi))
log.info('TEST: Metrics.density = %f' % (Metrics.density))
log.info('TEST: Metrics.fontscale = %f' % (Metrics.fontscale))
from kivy.setupconfig import USE_SDL2
log.info('TEST: USE_SDL2 = %d',USE_SDL2)
log.info('TEST: Metrics.dp = %f' % (Metrics.dp))
log.info('TEST: metrics.dp(1.01) = %f' % (dp(1.01)))

03-10 17:05:29.968 19755 19793 I python  : [INFO   ] [Text        ] Provider: sdl2
03-10 17:05:29.973 19755 19793 I python  : [INFO   ] [Audio       ] Providers: audio_sdl2 (audio_android, audio_ffpyplayer ignored)
03-10 17:05:29.995 19755 19793 I python  : [INFO   ] [TEST        ] device screen width 6.1 cm, HANDY detected
03-10 17:05:30.012 19755 19793 I python  : [INFO   ] [TEST        ] Metrics.dp = -1.000000
03-10 17:05:30.012 19755 19793 I python  : [INFO   ] [TEST        ] metrics.dp(1.01) = -1.010000

03-10 17:05:30.012 19755 19793 I python  : [INFO   ] [TEST        ] Metrics.sp = 1.000000
03-10 17:05:30.012 19755 19793 I python  : [INFO   ] [TEST        ] Metrics.dpi = 450.000000
03-10 17:05:30.017 19755 19793 I python  : [INFO   ] [TEST        ] Metrics.density = 2.812500
03-10 17:05:30.044 19755 19793 I python  : [INFO   ] [TEST        ] Metrics.fontscale = 1.100000
03-10 17:05:30.044 19755 19793 I python  : [INFO   ] [TEST        ] USE_SDL2 = 1
03-10 17:05:30.044 19755 19793 I python  : [INFO   ] [TEST        ] Metrics.dp = -1.000000
03-10 17:05:30.044 19755 19793 I python  : [INFO   ] [TEST        ] metrics.dp(1.01) = 2.840625


and couldn't believe my eyes. Now the whole app runs correctly, and the "width = 0" runtime error is also gone.

Unfortunately, it looks like a nasty race condition to me, or something with the Kivy observer system for the metric data. The app with Python 2 and Kivy 1.1.1 has been running for years without a problem on various devices, slow and fast, tablets and mobile phones.

Maybe that was just a coincidence and I shouldn't load the KV too early?

elli...@cox.net

unread,
Mar 10, 2025, 5:47:11 PMMar 10
to kivy-...@googlegroups.com
That is strange... What is the order of your imports.  Some of the kivy imports do have side effects.  Are you loading your kv code prior to importing kivy.app?



From: 'Mike' via Kivy users support <kivy-...@googlegroups.com>
Sent: Monday, March 10, 2025 9:39 AM
To: Kivy users support <kivy-...@googlegroups.com>
Subject: [kivy-users] Re: Metrics.dp < 0?
 
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/kivy-users/a3dc8d06-712d-4a5f-9065-67ed8f87d52cn%40googlegroups.com.

Mike

unread,
Mar 11, 2025, 11:16:53 AMMar 11
to Kivy users support
No, the first Kivy related imports are

from kivy.config import Config

from kivy.app import App

from kivy.core.window import Window

Window.clearcolor = (1, 1, 1, 1)


from kivy.base import EventLoop

from kivy.uix.scrollview import ScrollView

from kivy.uix.gridlayout import GridLayout

from kivy_hoverable import HoverBehavior # @UnusedImport

from kivy.uix.behaviors import ButtonBehavior

from kivy.uix.label import Label

from kivy.properties import ObjectProperty

from kivy.clock import Clock

from kivy.utils import platform as kivyplatform


from gui_base import ...


The last ist the first with KV loading on import and the shown log outputs (after KV loading).

elli...@cox.net

unread,
Mar 11, 2025, 3:25:50 PMMar 11
to Kivy users support
I don't have a clue.  You could bind to Metrics.dp and see when it is changing... I've also never seen dp as a negative number.  I would guess -1 indicated an error condition.  I suggest looking at the source code.  Very strange.

This is just a wild guess:
It could be that if you really have a literal in the dp, that it is getting processed when the kv code is parsed prior to the dpi being set.  You could try initializing a kivy property with the literal, that might delay the evaluation. 

Are you really using a literal of 1.01?


From: 'Mike' via Kivy users support <kivy-...@googlegroups.com>
Sent: Tuesday, March 11, 2025 4:16 AM

To: Kivy users support <kivy-...@googlegroups.com>
Subject: Re: [kivy-users] Re: Metrics.dp < 0?
 

Mike

unread,
Mar 13, 2025, 3:01:41 PMMar 13
to Kivy users support
> Are you really using a literal of 1.01?

Yes, but this is an "ancient" artefact. In a very old version of Kivy, the behaviour of ‘width: dp(1)’ was different from what was displayed with ‘width: dp(1.01)’ - the desired / awaited one.

Sirpo Nenchadirka nuhu

unread,
Mar 14, 2025, 3:34:46 PMMar 14
to Kivy users support
Please create a ticket and relate with the team for support

Sirpo Nenchadirka nuhu

unread,
Mar 14, 2025, 3:35:01 PMMar 14
to Kivy users support
Please contact support
On Thursday, 13 March 2025 at 16:01:41 UTC+1 Mike wrote:

Mike

unread,
Mar 17, 2025, 1:09:28 PM (12 days ago) Mar 17
to Kivy users support
I'm sorry, but I don't have discord access at the moment. Verification via phone number fails because an old account already has the same number. Unfortunately, logging into the old account doesn't work either.

I will try to find out more, e.g. whether this is related to loading the kv too early or whether the accesses alone ‘switch’ something instead of the log outputs.

But I just don't have the time at the moment, and I'm on holiday for the next 3 weeks.

Translated with DeepL.com (free version)
Reply all
Reply to author
Forward
0 new messages