在下面的代码中我应该怎么做才能使矩形背景颜色不同于背景颜色?我相信这里的问题是ScrollView
,因为当我将其更改为RelativeLayout
或BoxLayout
时,我可以绘制矩形。
from kivy.config import Config
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Rectangle
import copy
Config.set('graphics', 'width', '1280')
Config.set('graphics', 'height', '720')
Config.write()
kv = '''
Main:
ScrollView:
canvas.before:
Color:
rgba: (0.2, 0.745, 0.745, 1)
Rectangle:
pos: self.pos
size: self.size
StackLayout:
canvas.before:
Color:
rgba: (0.2, 0, 0.745, 1)
height: max(self.minimum_height, root.height)
size_hint_y: None
'''
class Main(BoxLayout):
def __init__(self, **kwargs):
super(Main, self).__init__(**kwargs)
self.touch_down_pos = [0, 0]
def on_touch_down(self, touch):
self.touch_down_pos = copy.deepcopy(touch)
with self.canvas:
touch.ud['rectangle'] = Rectangle(pos=(touch.x, touch.y), size=(0, 0))
def on_touch_move(self, touch):
touch.ud['rectangle'].size = (touch.x - self.touch_down_pos.x, touch.y - self.touch_down_pos.y)
return super(Main, self).on_touch_move(touch)
sm = Builder.load_string(kv)
class BajotApp(App):
def build(self):
return sm
if __name__ == '__main__':
BajotApp().run()
答案 0 :(得分:1)
只需将Color
添加到您的on_touch_down()
方法中即可:
def on_touch_down(self, touch):
self.touch_down_pos = copy.deepcopy(touch)
with self.canvas:
Color(0,1,0,1)
touch.ud['rectangle'] = Rectangle(pos=(touch.x, touch.y), size=(0, 0))
return super(Main, self).on_touch_down(touch)