繁体   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