Python:如何在 kivy 中使标签加粗

Posted

技术标签:

【中文标题】Python:如何在 kivy 中使标签加粗【英文标题】:Python : How to make label bold in kivy 【发布时间】:2019-02-12 18:54:13 【问题描述】:

我在python-2.7kivy 中使用Popup 小部件。有人可以帮助我吗? 1.如何使标签加粗? (例如text: "make label bold") 2. 如何更改标题颜色? (例如title : "change title color"

test.py

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.popup import Popup

class abc(Popup):
    def __init__(self, **kwargs):
        super(abc, self).__init__(**kwargs)
        self.open()


class TestApp(App):
    def build(self):
        return abc()


TestApp().run()

test.kv

<abc>
    title : "change title color"
    BoxLayout:
        orientation: "vertical"
        GridLayout:
            Label:
                text: "make label bold"

【问题讨论】:

【参考方案1】:

粗体标签文本

有两种方法可以使标签的文本变为粗体。它们如下:

方法一

使用bold: True

Label:
    bold: True

Label » bold

bold

表示使用粗体字体。

注意

根据您的字体,粗体属性可能对您的字体没有影响 文本渲染。

bold 是 BooleanProperty,默认为 False。

方法二

使用Markup text、markup: True

Label:
    markup: True
    text: '[b]make label bold[/b]

更改标题颜色

使用title_color

<abc>
    title : "change title color"
    title_color: [1, 0, 0, 1]    # red title

Popup » title_color

title_color

标题使用的颜色。

title_color 是一个 ListProperty,默认为 [1, 1, 1, 1]。

示例

main.py

​​>
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.lang import Builder


Builder.load_string('''
#:kivy 1.11.0

<abc>
    title : "change title color"
    title_color: 1, 0, 0, 1    # red title
    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 1
            Label:
                bold: True
                text: "make label bold"

            Label:
                markup: True
                text: "[b]make label bold[/b]"

''')


class abc(Popup):
    pass


class PopupApp(App):
    title = 'Popup Demo'

    def build(self):
        self._popup = abc()
        return Button(text="press me", on_press=self._popup.open)


PopupApp().run()

输出

【讨论】:

【参考方案2】:

您可以参考APIhere

    它是一个布尔值,默认为 False,因此您可以用作: Label: text: 'blah blah' bold: True

    您可以尝试使用以下标签添加颜色:

    color: [0.941, 0, 0,1]

这应该以红色显示。它使用 RGBA 格式(A 表示 Alpha/Opacity)。您可以使用this 工具来选择您的颜色。

【讨论】:

以上是关于Python:如何在 kivy 中使标签加粗的主要内容,如果未能解决你的问题,请参考以下文章

如何在运行时在android中使部分文本加粗?

如何在kivy的gridlayout中使小部件跨越多个列/行

如何在monthCalendar 控件中使每个星期一加粗? [复制]

KIVY python:在python代码中使按钮可点击

如何在 Kivy、Python 中更新标签的文本

如何在kivy python中添加标签,TextInput