簡體   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