Search code examples
pythonkivykivy-languagekivymd

how to add elif statement in kivy language?


Have a good day everybody, I want to ask that how to add elif statement in Kivy language. Here is the code in my .kv file:

MDCard:
    size_hint: None, None
    size: 232.5, 23
    orientation: "vertical"
    pos: 352.5, 255 + 23/2
    md_bg_color: [200/255, 200/255, 0/255, 1] if app.weekday == "2" else [0/255, 200/255, 0/255, 1]

I want to add a condition that if app.weekday == "3": md_bg_color: [200/255, 0/255, 0/255, 1]

Thank you very much.


Solution

  • Hi i'll put this as an anwer.

    1. That's normal python languaje, and it's an "if one-liner"
    2. There's no such thing as if-elif-else one-liner but you can use some if-else concatenated
    3. I would NOT recomend that.

    4)You can create a default dict and access it by day to select your color:

    from collections import defaultdict
    #create default dict and default color
    default_color = [0/255, 200/255, 0/255, 1]
    color_dict_by_day = defaultdict(lambda: default_color)
    
    #add other colors by key as "num"
    
    color_dict_by_day["2"] = [200/255, 200/255, 0/255, 1]
    color_dict_by_day["3"] = [200/255, 0/255, 0/255, 1]
    # and so on....
    
    #for last call your code block as:
    
    MDCard:
        size_hint: None, None
        size: 232.5, 23
        orientation: "vertical"
        pos: 352.5, 255 + 23/2
        md_bg_color: color_dict_by_day[app.weekday]