簡體   English   中英

在kivyMD的MDToolbar中更改圖標的顏色?

[英]change color of icon in MDToolbar in kivyMD?

我想更改 MDToolbar left_action_item 圖標顏色。 它默認為白色,但現在我想將其更改為紅色。 最簡單的方法是什么? 我幾乎嘗試了所有東西(text_color、bg_color 等),但都無濟於事。

您無法更改工具欄中圖標的顏色。

在這種情況下,我建議在KivyMD 存儲庫中搜索相關的小部件 class,然后四處尋找它是如何定義的,相關的 ID 是什么等等。 例如, toolbar.py中的這一行似乎定義了工具欄中的圖標:

def update_action_bar(self, action_bar, action_bar_items):
    #...
    action_bar.add_widget(
        MDIconButton(
            icon=item[0],
            on_release=item[1],
            opposite_colors=True,
            text_color=self.specific_text_color,
            theme_text_color="Custom",
        )
    )
    #...

在這里,我們了解到工具欄的圖標是 class MDIconButton ,它們有一個text_color顏色屬性,似乎正在設置顏色。

查看上面的 function 的調用位置,我們看到這些圖標分別作為小部件添加到self.ids["left_actions"]self.ids["right_actions"]中:

def on_left_action_items(self, instance, value):
    self.update_action_bar(self.ids["left_actions"], value)

def on_right_action_items(self, instance, value):
    self.update_action_bar(self.ids["right_actions"], value)

知道了這一切,現在在我們自己的代碼中,比如在 MainApp 的build() MainApp中,我們可以訪問和修改屬性:

def build(self):
    # ... 
        
    # get the root widget
    self.root = root = Builder.load_file('root.kv')

    # get toolbar
    toolbar=root.ids.toolbar
    
    # get the icons on the right
    action_items = toolbar.ids.right_actions.children

    # loop over the icons
    for item in action_items:
        # change the color
        item.text_color=(1,0,0,1) # red

這不需要在 build() 中,它只需要在某個地方,您可以通過它的 ID 以某種方式訪問工具欄小部件。

使用specific_text_color: 1,0,1,1您可以更改工具欄內文本的顏色。 它同時更改了文本和圖標。 我不知道如何只更改圖標。 也許這有幫助。

目前我無法更改OneLineIconListItem的圖標顏色。 我認為它與我們遇到的約束相同?

同時使用md_bg_color: app.theme_cls.primary_colortext_color: rgba('#F0F0F0')允許我更改 MDToolbar 中圖標按鈕的顏色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM