Reputation: 824
In example below, there are two rectangles drawn in canvas of FloatLayout.
The goal is to create something like a simple pixel art drawing app where the user can draw rectangles and change their color (for example color of rectangle under mouse), so I can’t create these rectangles in kv file.
So in this demo example I just want to change the color of rectangle under the mouse.
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.graphics import Color, Rectangle
KV = """
FloatLayout
size_hint: None, None
size: 512, 512
on_touch_down: app.test(*args[1].pos)
"""
class MyApp(App):
color = ListProperty((1,1,1,1))
def build(self):
self.root = Builder.load_string(KV)
self.init_rects()
def init_rects(self):
with self.root.canvas:
x,y = self.root.pos
w,h = self.root.size
Color(rgba=(1,1,1,1))
self.r1 = Rectangle(pos = (x,y), size= (w/2,h))
Color(rgba=(1,0,0,1))
self.r2 = Rectangle(pos = (w/2,y), size= (w/2,h))
def test(self, x,y):
if x< self.root.center_x:
print ('I need to color this rectangle (self.r1) to red')
else:
print ('I need to color this rectangle (self.r2) to white')
MyApp().run()
In this example I store rectangles as self.r1 and self.r2 (because I think further I will need to change them pos or size)
The problem is I didn't find an example of how to change only one rectangle color, and not to change other colors.
I have a stupid solution (below) - every time to create a new rectangle. But I am sure that this is a bad solution when there will be a lot of rectangles
def test(self, touch_x, touch_y):
with self.root.canvas:
x,y = self.root.pos
w,h = self.root.size
if touch_x< self.root.center_x:
Color(rgba=(1,0,0,1))
self.r1 = Rectangle(pos = (x,y), size= (w/2,h))
else:
Color(rgba=(1,1,1,1))
self.r2 = Rectangle(pos = (w/2,y), size= (w/2,h))
Roughly speaking I miss something like Rectangle(rgba=...)
What could be the solution in this case?
Upvotes: 0
Views: 277
Reputation: 38837
You can change the Color
instead of trying to change the Rectangle
. Here is a modification of your code that demonstrates this:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.graphics import Color, Rectangle
KV = """
FloatLayout
size_hint: None, None
size: 512, 512
on_touch_down: app.test(*args[1].pos)
"""
class MyApp(App):
color = ListProperty((1,1,1,1))
def build(self):
self.root = Builder.load_string(KV)
self.init_rects()
def init_rects(self):
with self.root.canvas:
x,y = self.root.pos
w,h = self.root.size
self.c1 = Color(rgba=(1,1,1,1))
Rectangle(pos = (x,y), size= (w/2,h))
self.c2 = Color(rgba=(1,0,0,1))
Rectangle(pos = (w/2,y), size= (w/2,h))
def test(self, x,y):
if x< self.root.center_x:
print ('I need to color this rectangle (self.r1) to red')
self.c1.rgba = (1,0,0,1)
else:
print ('I need to color this rectangle (self.r2) to white')
self.c2.rgba = (1,1,1,1)
MyApp().run()
Upvotes: 1