【问题标题】:How to change font size dynamically in 'Text Markup'?如何在“文本标记”中动态更改字体大小?
【发布时间】:2019-06-22 06:24:16
【问题描述】:

我想在Text Markup 中动态更改字体大小:https://kivy.org/doc/stable/api-kivy.core.text.markup.html

下面的代码运行良好。

str_ = "[size=17sp]" + 'TEST' + "[/size]"

下面的代码效果不好。

from kivy.metrics import sp
font_size = sp(17)
str_ = "[size=font_size]" + 'TEST' + "[/size]"

我应该如何修改这个?还是用Text Markup 不可能实现?

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    该问题有三种可能的解决方案,如下所示。

    片段

    方法 1 - 分割标记

    from kivy.core.text.markup import MarkupLabel
    
            markup_splitted = MarkupLabel(self.ids.label.text).markup
    
            font_size = '50sp'
            self.ids.label.text = ''
    
            for item in markup_splitted:
                if item[:6] == '[size=':
                    self.ids.label.text += f'[size={font_size}]'
                else:
                    self.ids.label.text += item
    

    方法 2 - 不带sp 的整数值

    font_size = 17
    str_ = f"[size={font_size}]" + 'TEST' + "[/size]"
    

    方法 3 - 带有sp 的字符串值

    font_size = '17sp'
    str_ = f"[size={font_size}]" + 'TEST' + "[/size]"
    

    示例

    以下说明了解决问题的三种方法。

    main.py

    ​​>
    from kivy.base import runTouchApp
    from kivy.lang import Builder
    from kivy.uix.screenmanager import Screen
    from kivy.properties import StringProperty
    from kivy.core.text.markup import MarkupLabel
    
    
    class MarkupTextFontSize(Screen):
        txt = StringProperty('')
    
        def __init__(self, **kwargs):
            super(MarkupTextFontSize, self).__init__(**kwargs)
            self.txt = '[color=ff3333]Unselectable [size=20]item[/size][/color][anchor=a]a\nChars [anchor=b]b\n[ref=myref]ref[/ref]'
            # self.txt += "[anchor=title1][size=24]This is my Big title.[/size][anchor=content] Hello world"
    
        def change_font_size(self):
            self.ids.label.font_size = '50sp'
    
            # Method 1 - split markup
            markup_splitted = MarkupLabel(self.ids.label4.text).markup
    
            font_size = '50sp'
            self.ids.label4.text = ''
    
            for item in markup_splitted:
                if item[:6] == '[size=':
                    self.ids.label4.text += f'[size={font_size}]'
                else:
                    self.ids.label4.text += item
    
            # Method 2 - using integer value
            font_size = 17
            self.ids.label2.text = f"[size={font_size}]" + 'TEST' + "[/size]"
    
            # Method 3 - using string value
            font_size = '30sp'
            self.ids.label3.text = f"[anchor=title1][size={font_size}]This is my Big title.[/size][anchor=content] Hello world"
    
    
    runTouchApp(Builder.load_string("""
    MarkupTextFontSize:
    
    <MarkupTextFontSize>:
        BoxLayout:
            orientation: 'vertical'
    
            Button:
                id: btn
                text: 'Change FontSize'
                size_hint: 1, 0.2
                markup: True
                on_release: root.change_font_size()
            Label:
                id: label
                text: root.txt
                markup: True
            Label:
                id: label2
                text: '[color=ff3333]Unselectable [size=20sp]item[/size][/color]'
                markup: True
            Label:
                id: label3
                text: "[anchor=title1][size=24]This is my Big title.[/size][anchor=content] Hello world"
                markup: True
            Label:
                id: label4
                text: "[anchor=title1][size=24]This is my Big title.[/size][anchor=content] Hello world"
                markup: True
    """))
    

    输出

    【讨论】:

    • 谢谢!!解释得真好!
    【解决方案2】:

    尝试像这样插入字符串:

    from kivy.metrics import sp
    font_size = sp(17)
    str_ = f'[size={font_size}]' + 'TEST' + "[/size]"
    

    【讨论】:

    • 谢谢。我试过了,但我得到了 'SyntaxError: invalid syntax'
    猜你喜欢
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2022-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多