如何在 kivy 应用程序中正确为 MDLabel 的背景着色

发布于 2025-01-12 23:09:00 字数 1546 浏览 0 评论 0原文

尝试在 kivy 应用程序中为 MDLabel(标签)的背景着色。但无法将标签的宽度调整为与文本(内容)的大小完全相同。

GridLayout:
    cols: 1
    BoxLayout:
        orientation: 'vertical'
        size_hint_y: None
        padding: dp(7)
        # pos_hint: {'top': 1}
        background_color: (1,1,0,1)
        height: self.minimum_height
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos
        BoxLayout:
            size_hint_y: None
            # pos_hint: {'top': 1}
            height: self.minimum_height
            background_color: (0,1,0,1)
            canvas.before:
                Color:
                    rgba: self.background_color
                Rectangle:
                    size: self.size
                    pos: self.pos
            MDLabel:
                text: 'Some text'
                size_hint_y: None
                size: self.texture_size
            MDLabel:
                id: trying_to_color_background
                text: 'Width'
                halign: 'right'
                size_hint_x: None
                size: self.texture_size

得到这个 在此处输入图像描述

如果我尝试删除代码的最后一行“size: self.texture_size” ”。看起来像这样 在此处输入图像描述

但我希望背景是文本内容的大小(大小下图中的红色边框)。如果文本中有更多字母,我希望背景更宽 在此处输入图片说明

Trying to color background of MDLabel (Label) in kivy app. But can not adjust width of the Label to be exactly the size of the text (content).

GridLayout:
    cols: 1
    BoxLayout:
        orientation: 'vertical'
        size_hint_y: None
        padding: dp(7)
        # pos_hint: {'top': 1}
        background_color: (1,1,0,1)
        height: self.minimum_height
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos
        BoxLayout:
            size_hint_y: None
            # pos_hint: {'top': 1}
            height: self.minimum_height
            background_color: (0,1,0,1)
            canvas.before:
                Color:
                    rgba: self.background_color
                Rectangle:
                    size: self.size
                    pos: self.pos
            MDLabel:
                text: 'Some text'
                size_hint_y: None
                size: self.texture_size
            MDLabel:
                id: trying_to_color_background
                text: 'Width'
                halign: 'right'
                size_hint_x: None
                size: self.texture_size

getting this
enter image description here

if i try to remove last row of the code "size: self.texture_size". it looks like this
enter image description here

but i want the background to be the size of the text content (size of red border in the image below). if there will be more letters in the text, i want the background to be wider
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2025-01-19 23:09:00

将其设置为,

            ...
            MDLabel:
                id: trying_to_color_background
                text: 'Width'
                halign: 'right'
                size_hint: None, None
                size: self.texture_size
                text_size: None, None

Set it as,

            ...
            MDLabel:
                id: trying_to_color_background
                text: 'Width'
                halign: 'right'
                size_hint: None, None
                size: self.texture_size
                text_size: None, None
似梦非梦 2025-01-19 23:09:00
lbl_state = MDLabel(text="state", md_bg_color = [.119, .136, .153, 1])
lbl_state = MDLabel(text="state", theme_text_color = "Custom", md_bg_color = [.119, .136, .153, 1])
from kivy.utils import get_color_from_hex

lbl_state = MDLabel(text="state", theme_text_color = "Custom", md_bg_color = get_color_from_hex("#7dcbb1"))
MDLabel:
   id: trying_to_color_background
   theme_text_color: "Custom"
   md_bg_color = .119, .136, .153, 1

不同颜色的示例:

0, 0, 0, 1 - dark
1, 1, 1, 1 - white
.7, .7, .7, 1 - gray
1, 0, 0, 1 - red
0, 1, 0, 1 - green
0, 0, 1, 1 - blue
lbl_state = MDLabel(text="state", md_bg_color = [.119, .136, .153, 1])
lbl_state = MDLabel(text="state", theme_text_color = "Custom", md_bg_color = [.119, .136, .153, 1])
from kivy.utils import get_color_from_hex

lbl_state = MDLabel(text="state", theme_text_color = "Custom", md_bg_color = get_color_from_hex("#7dcbb1"))
MDLabel:
   id: trying_to_color_background
   theme_text_color: "Custom"
   md_bg_color = .119, .136, .153, 1

Example for different colors:

0, 0, 0, 1 - dark
1, 1, 1, 1 - white
.7, .7, .7, 1 - gray
1, 0, 0, 1 - red
0, 1, 0, 1 - green
0, 0, 1, 1 - blue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文