繁体   English   中英

如何在 Kivy/KivyMD 中更改 MDRectangleFlatIconButton 文本颜色

[英]How to change MDRectangleFlatIconButton text colour in Kivy/KivyMD

我正在使用 Kivy 和 KivyMD 创建布局,并希望更改 MD 按钮中显示的文本的颜色,但颜色仍然停留在浅蓝色上。

我在下面的代码中包含了一个我尝试过的例子。

.py代码

import kivy, kivymd

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.theming import ThemeManager


class ButtonColorApp(App):

    theme_cls = ThemeManager()
    title='RUCES'

    def build(self):
        self.theme_cls.theme_style = "Dark"
        sm = ScreenManager()
        sm.add_widget(IntroPage(name="intro_page"))
        return sm

class IntroPage(Screen):

    #funcs and vars
    pass

def main():
        but = ButtonColorApp()
        but.run()

if __name__ == "__main__":
        main()

.kv代码

#: import MDRectangleFlatIconButton kivymd.button.MDRectangleFlatIconButton
#: import MDLabel kivymd.label.MDLabel

<MyButton@MDRectangleFlatIconButton>:
    text_size: self.size * 3
    theme_text_color: 'Custom'
    font_size: 20
    md_bg_color: (0,0,.4,1)
    canvas.before:
       Color:
            rgba: (0,0,0,1)
            Line:
                width: 0.5
                rectangle: (self.x, self.y, self.width, self.height)


<IntroPage>:

    BoxLayout:

        orientation: "vertical"

        MyButton:
            size_hint_x: 1
            theme_text_color: 'Custom'
            text: "Colour Me!"
            text_color: (1,0,0,1)

当我运行它时,我希望按钮文本为红色,但如上所述它仍然是浅蓝色。 任何帮助表示赞赏!

问题

目前, MDRectangleFlatIconButtonMDRoundFlatIconButton小部件的文本颜色将始终默认为theme_cls.primary_color即使具有属性theme_text_color: 'Custom'text_color: [1,0,0,1]

解决方案

临时解决方法如下:

  1. 在您的 kv 文件中,将 import 语句kivymd.button.MDRectangleFlatIconButton替换为您自定义的button.py,button.MDRectangleFlatIconButton
  2. 我的 GitHub中获取button.py的副本
  3. 或者从/usr/local/lib/python3.7/dist-packages/kivymd~/KivyMD (由git clone https://github.com/HeaTTheatR/KivyMD.git创建) ~/KivyMD button.py的副本,或下载并解压缩 KivyMD Zip 文件(下载 ZIP ),并应用以下更改

代替:

        theme_text_color: 'Custom'
        text_color: root.theme_cls.primary_color

和:

        theme_text_color: root.theme_text_color   
        text_color: root.text_color                 

片段:button.py - kv

<MDRectangleFlatIconButton>
    ...
    theme_text_color: 'Custom'
    text_color: root.theme_cls.primary_color

    BoxLayout:
        ...

        MDIcon:
            ...

        MDLabel:
            ..
            theme_text_color: root.theme_text_color    
            text_color: root.text_color                 
            markup: root.markup

<MDRoundFlatIconButton>
    ...
    theme_text_color: 'Custom'
    text_color: root.theme_cls.primary_color

    BoxLayout:
        ...
        MDIcon:
            ...

        MDLabel:
            ...
            theme_text_color: root.theme_text_color     
            text_color: root.text_color                 
            markup: root.markup

输出

结果

KivyMD GitHub

另一种解决方法是使用标记语言,例如:

  1. 在您的.kv文件中,将"id"分配给您的MDButton
MDFlatButton:

    id: info_btn
  1. 然后在你的.py文件中
self.ids.info_btn.text = '[color=#00ffcc]Info[/color]'

self.ids.info_btn.markup = True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM