[英]Dynamically changing rectangles colors
In example below, there are two rectangles drawn in canvas of FloatLayout. 在下面的示例中,在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. 目标是创建类似于简单像素艺术绘图应用程序的程序,在该程序中,用户可以绘制矩形并更改其颜色(例如,鼠标下的矩形颜色),因此我无法在kv文件中创建这些矩形。
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) 在此示例中,我将矩形存储为self.r1和self.r2(因为我认为进一步需要更改pos或大小)
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=...)
粗略地说我想念
Rectangle(rgba=...)
What could be the solution in this case? 在这种情况下,解决方案是什么?
You can change the Color
instead of trying to change the Rectangle
. 您可以更改
Color
而不是尝试更改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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.