Reputation: 6185
I would like to add a canvas to my checkboxes to change their color. I have found this answer, but I am struggling to implement it. My checkboxes are created in python with this code:
checkb= CheckBox()
layout.add_widget(checkb)
Attempt 1: I tried the solution from here but without success:
checkb= CheckBox()
checkb.canvas.add(Color(1., 1., 0))
checkb.canvas.add(Rectangle(size=(50, 50)))
layout.add_widget(checkb)
Attempt 2: I also tried to come up with a custom checkbox created in the builder but didn't find a way to make it work (I couldn't find any info about this kind of setup, so I am not sure it's even possible to make it work):
Builder.load_string('''
<CustomCk@CheckBox>:
canvas.before:
Color:
rgb: 1,0,0
Rectangle:
pos:self.center_x-8, self.center_y-8
size:[16,16]
Color:
rgb: 0,0,0
Rectangle:
pos:self.center_x-7, self.center_y-7
size:[14,14]
''')
and
checkb= CustomCk()
layout.add_widget(checkb)
Edit : my try with the with statement:
checkb= CheckBox()
with checkb.canvas:
Color(1, 2, 0)
Rectangle(size=(50, 50))
layout.add_widget(checkb)
Upvotes: 0
Views: 1677
Reputation: 8066
You should use the with statement from your python code
with checkb.canvas:
Color(1., 1., 0)
Rectangle(size=(50, 50))
Your other approach seems better, just fix it a bit:
Builder.load_string('''
<CustomCk>:
canvas.before:
Color:
rgb: 1,0,0
Rectangle:
pos:self.center_x-8, self.center_y-8
size:[16,16]
Color:
rgb: 0,0,0
Rectangle:
pos:self.center_x-7, self.center_y-7
size:[14,14]
''')
class CustomCk(CheckBox): #define the class in the python file...
pass
checkb= CustomCk()
layout.add_widget(checkb)
Upvotes: 1