Search code examples
pythonkivykivymd

Kivymd: change toolbar backgroud color by pressing a button


I'd like to know of there's possible to change Toolbar Background Color by pressing a button. I have 6 buttons with different colors, and when I press it, the toolbar background changes it's colors...

I tried to change creating a method, but returns a error...

def changeThemeColor(self):
        self.root.ids.tbCategorie.md_bg_color = [0, 0, 0, 1]

Here's my code below.

from kivymd.app import MDApp
from kivymd.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivymd.uix.button import MDFlatButton
from kivy.uix.scrollview import ScrollView

KV = '''
ScreenManager:
    Screen:
        name: 'screenOption'
        BoxLayout:
            orientation: 'vertical'
            MDToolbar:
                id: tbOption
                md_bg_color: 0, 0, 0, 1
            Options:
                id: screenoption

<Options>
    ScrollView:
        MDList:
            spacing: '20dp'
            padding: '25dp'
            
            MDLabel:    
            BoxLayout:
                MDFlatButton:
                    md_bg_color: (0/255,0/255,0/255)
                    on_release: app.changeThemeColor()
                    line_color: (128/255,128/255,128/255)
                
                MDLabel:        
                MDFlatButton:
                    md_bg_color: (255/255,255/255,255/255)
                    line_color: (128/255,128/255,128/255)
                
                MDLabel:    
                MDFlatButton:
                    md_bg_color: (255/255,0/255,0/255)
                    line_color: (128/255,128/255,128/255)
                
                MDLabel:
                MDFlatButton:
                    md_bg_color: (0/255,0/255,255/255)
                    line_color: (128/255,128/255,128/255)
            
            MDLabel:
            BoxLayout:
                MDFlatButton:
                    md_bg_color: (255/255,0/255,255/255)
                    line_color: (128/255,128/255,128/255)
                
                MDLabel:
                MDFlatButton:
                    md_bg_color: (255/255,255/255,0/255)
                    line_color: (128/255,128/255,128/255)
                
                MDLabel:
                MDFlatButton:
                    md_bg_color: (0/255,128/255,0/255)
                    line_color: (128/255,128/255,128/255)
                    
                MDLabel:
                MDFlatButton:
                    md_bg_color: (128/255,128/255,128/255)
                    line_color: (128/255,128/255,128/255)
'''

class Options(FloatLayout):
    pass
    
class Aplicativo(MDApp):
    
    def changeThemeColor(self):
        self.root.ids.tbCategorie.md_bg_color = [0, 0, 0, 1]
    
    def build(self):
        return Builder.load_string(KV)
        
Aplicativo().run()

By the way, the original code I have at least 5 different toolbar I'd like to change.


Solution

  • In your posted code tbOption refers to the MDToolbar while tbCategorie has no reference. So in method changeThemeColor you need to change that as,

    self.root.ids.tbOption.md_bg_color = [0, 0, 0, 1]
    

    Additionally you might get the following error while setting the md_bg_color,

    ValueError: MDToolbar.md_bg_color value length is immutable`
    

    which means that md_bg_color should be a list or tuple (if given in this format other than string, None etc.) of four values between 0 and 1 in [r, g, b, a] format.