如何在KV中定位canvas.before [英] How to target canvas.before in kv
问题描述
在按钮中,我使用 canvas.before
制作了一个圆形按钮,它会根据需要更改颜色.该行是:
In a button i have made a rounded button with canvas.before
, and it changes colors as it should. The line is:
canvas.before:
Color:
rgba: btn_color_not_pressed if self.state=='normal' else btn_color_pressed
RoundedRectangle:
size: self.size
pos: self.pos
radius: [40]
变量 btn_color_not_pressed 和 btn_color_not_pressed 是在kv文件的开头使用#:set
进行的
The variables btn_color_not_pressed and btn_color_not_pressed are made with #:set
in the start of the kv-file
我已经尝试使用 self.canvas.before.Color.rgba
定位行,就像我通常那样,但是出现以下错误:
I have tried to target the line with self.canvas.before.Color.rgba
, as i am used to normally, but i get following error:
AttributeError:'kivy.graphics.instructions.CanvasBase'对象没有属性'Color'
我该如何在kv中定位该行并替换变量...或在必要时从python文件中替换该变量?如何在 Rectangle
下指定 source:"some_file.jpg
"?
How do i target that line from within kv and replace the variables ... or if necessary from the python file.?
How do i target the source: "some_file.jpg
under Rectangle
?
我的目标是,当用户单击某个选项时,应用程序中的所有按钮颜色(也许是背景)都必须更改.
My goal is that when a user has clicked an option all the button colors (and maybe the background) in the app must change.
推荐答案
找到了解决方案(此处: Kivy:在重新进入屏幕时将切换按钮重置为正常" )
Found a solution (here: Kivy: resetting toggle buttons to "normal" on re-entering screen)
这有点丑陋,但是可以..
It's kind'a ugly, but it works..
给每个按钮一个id ...,然后对每个按钮使用 on_enter
并设置和更改状态.
Give every button an id ... and then use on_enter
for each button and set and change the state.
在上面的代码中,其含义是:
In the code above it would mean:
on_enter:
button1.state = "down"
button1.state = "normal"
button2.state = "down"
button2.state = "normal"
button3.state = "down"
button3.state = "normal"
button4.state = "down"
button4.state = "normal"
它可以工作...但是它不是很漂亮:|
It works ... but it is not pretty :|
这篇关于如何在KV中定位canvas.before的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!