【问题标题】:change color of icon in MDToolbar in kivyMD?在kivyMD的MDToolbar中更改图标的颜色?
【发布时间】:2020-11-23 00:03:45
【问题描述】:

我想更改 MDToolbar left_action_item 图标颜色。它默认为白色,但现在我想将其更改为红色。最简单的方法是什么?我几乎尝试了所有方法(text_color、bg_color 等),但均无济于事。

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    您无法更改工具栏中图标的颜色。

    【讨论】:

    • 如果那不可能,那我该如何放置自己的自定义图标呢?
    • 那是另一个问题。
    【解决方案2】:

    在这种情况下,我建议在 KivyMD repository 中搜索相关的小部件类,然后四处寻找它是如何定义的,相关的 ID 是什么等等。例如,toolbar.py 中的this line 似乎定义了工具栏中的图标:

    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",
            )
        )
        #...
    

    在这里我们了解到工具栏的图标属于MDIconButton 类,它们有一个text_color 颜色属性,似乎是在设置颜色。

    查看where the function above is called,我们看到这些图标分别作为小部件添加到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)
    

    知道了这一切,现在在我们自己的代码中,比如在我们的MainAppbuild() 函数中,我们可以访问和修改属性:

    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 以某种方式访问​​工具栏小部件。

    【讨论】:

      【解决方案3】:

      使用specific_text_color: 1,0,1,1,您可以更改工具栏中文本的颜色。它同时更改了文本和图标。我不知道如何只更改图标。也许这会有所帮助。

      目前我无法更改OneLineIconListItem 的图标颜色。我认为它与我们遇到的约束相同?

      【讨论】:

        【解决方案4】:

        同时使用 md_bg_color: app.theme_cls.primary_colortext_color: rgba('#F0F0F0') 允许我更改 MDToolbar 中图标按钮的颜色。 p>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-12-01
          • 2022-08-15
          • 2020-11-22
          • 1970-01-01
          • 1970-01-01
          • 2020-11-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多