【发布时间】:2021-08-17 13:12:22
【问题描述】:
环顾四周,似乎找不到任何关于通过引用 id: 在 Kivy 中更改画布颜色的可靠信息。我的目标是为我的应用程序创建一个明暗主题,其中用户在应用程序设置中更改它。这是我目前的 .kv 代码:
themescreen.kv
<ThemeScreen>:
id: theme_canvas
canvas:
Color:
rgb: utils.get_color_from_hex("#161D39")
Rectangle:
size: self.size
pos: self.pos
FloatLayout:
RoundedButton:
id: dark_theme
text: "Dark"
pos_hint: {"top": .5, "center_x": .5}
size_hint: .38, .08
padding: 20, 20
opacity: 1 if self.state == 'normal' else .5
on_release:
app.change_theme("#000523")
RoundedButton:
id: light_theme
text: "Light"
pos_hint: {"top": .3, "center_x": .5}
size_hint: .38, .08
padding: 20, 20
opacity: 1 if self.state == 'normal' else .5
on_release:
app.change_theme("#C4CCFF")
所以我要做的是运行将我的十六进制颜色更改为适当主题的函数。下面是我的 .py 函数。
def change_theme(self, theme):
theme_canvas = self.root.ids["theme_screen"].ids["theme_canvas"]
theme_canvas.rgb = utils.get_color_from_hex(theme)
我尝试将 id: 放置在不同的位置,我尝试将 rgb 颜色纯粹放在 FloatLayout 区域中,并在画布中使用 self.color 等,然后调用该 id: 代替。目前,通过此设置,我收到错误消息:
theme_canvas =self.root.ids["theme_screen"].ids["theme_canvas"] KeyError: 'theme_canvas' 我已经厌倦了从函数中删除theme_canvas。
请问有什么办法可以吗?我只是希望能够通过我的函数更改 rgb 颜色。请帮忙。
【问题讨论】:
标签: python function canvas colors kivy