1 回答

TA贡献1825条经验 获得超6个赞
您可以更改Color而不是尝试更改Rectangle. 这是对您的代码的修改,演示了这一点:
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()
添加回答
举报