如何在kivy语言中添加elif语句?
how to add elif statement in kivy language?
祝大家愉快,我想问一下如何在Kivy语言中添加elif语句。
这是我的 .kv 文件中的代码:
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]
我想添加一个条件 if app.weekday == "3": md_bg_color: [200/255, 0/255, 0/255, 1]
非常感谢。
嗨,我会把这个作为答案。
- 这是正常的 python languaje,而且是“if one-liner”
- 没有 if-elif-else 单行这样的东西,但你可以使用一些 if-else 连接
- 我不会推荐那个。
4)您可以创建一个默认字典并按天访问它以 select 您的颜色:
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]
你可以像这样做你想做的事:
md_bg_color: [200/255, 200/255, 0/255, 1] if app.weekday == "2" else [0/255, 200/255, 0/255, 1] if app.weekday == "3" else [200/255, 0/255, 0/255, 1]
祝大家愉快,我想问一下如何在Kivy语言中添加elif语句。 这是我的 .kv 文件中的代码:
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]
我想添加一个条件 if app.weekday == "3": md_bg_color: [200/255, 0/255, 0/255, 1]
非常感谢。
嗨,我会把这个作为答案。
- 这是正常的 python languaje,而且是“if one-liner”
- 没有 if-elif-else 单行这样的东西,但你可以使用一些 if-else 连接
- 我不会推荐那个。
4)您可以创建一个默认字典并按天访问它以 select 您的颜色:
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]
你可以像这样做你想做的事:
md_bg_color: [200/255, 200/255, 0/255, 1] if app.weekday == "2" else [0/255, 200/255, 0/255, 1] if app.weekday == "3" else [200/255, 0/255, 0/255, 1]