How to use a variable in font [size=<integer>][/size] in markup text on Label?

76 views
Skip to first unread message

Henrik R.

unread,
Jan 29, 2021, 10:54:53 AM1/29/21
to Kivy users support
I want to control all font sizes globally in my app, so they scale well. So I have this kv:

#:set my_font_size sp(15)
# And I added this:
#:set large_font_size sp(30)

But this doesn't work:

ScalableLabel:
    markup: True
    text:
        "\n[color=#3333ff][b][size=int(my_font_size * 2)]MyApp[/size][/b][/color]\nChoose language:"

I get this error:

ValueError: invalid literal for int() with base 10: 'int(my_font_size * 2)'

These didn't work either:

[size=large_font_size]
[size=int(large_font_size)]

Is there a work around for this limitation?

Elliot Garbus

unread,
Jan 29, 2021, 11:06:18 AM1/29/21
to kivy-...@googlegroups.com
Try using an F string to create your markup. 

Sent from my iPhone

On Jan 29, 2021, at 8:55 AM, Henrik R. <henrik.r...@gmail.com> wrote:


--
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 on the web visit https://groups.google.com/d/msgid/kivy-users/c7a86015-b866-4fd7-958f-bba46ee7235cn%40googlegroups.com.

Henrik R.

unread,
Jan 29, 2021, 11:50:05 AM1/29/21
to Kivy users support
Do you mean this:
    text:
        f"\n[color=#3333ff][b][size=large_font_size]Geo-ESP Training[/size][/b][/color]\nChoose language:"
That didn't help.

Elliot Garbus

unread,
Jan 29, 2021, 12:08:30 PM1/29/21
to kivy-...@googlegroups.com
Put the font size in a variable.  In the f string,
{large_font}

Sent from my iPhone

On Jan 29, 2021, at 9:50 AM, Henrik R. <henrik.r...@gmail.com> wrote:



Henrik R.

unread,
Jan 29, 2021, 12:47:35 PM1/29/21
to Kivy users support
Oops! Sorry! Yes - this works:
text:
     f"[color=#3333ff][b][size={int(my_font_size*1.5)}]Geo-ESP Training[/size][/b][/color]\nChoose language:"

Elliot Garbus

unread,
Jan 29, 2021, 12:54:24 PM1/29/21
to kivy-...@googlegroups.com

Here you go…  If you want to change the font size dynamically you could make the font size a kivy property and use the string.format() method.

 

from kivy.app import App
from kivy.lang import Builder


kv =
"""
BoxLayout:
    Label:
        markup: True
        text: f'[color=#3333ff][b][size={app.large_font}]MyApp[/size][/b][/color]
\\nChoose language:'
"""

class MarkupApp(App):

   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.large_font = 40
       
self.small_font = 9

   
def build(self):
       
return Builder.load_string(kv)


MarkupApp().run()

--

Henrik R.

unread,
Jan 29, 2021, 1:14:13 PM1/29/21
to Kivy users support
Thank you very much!
I will remember that, if I want to let the user change font size!
Reply all
Reply to author
Forward
0 new messages