KIVY collide_point RelativeLayout vs StencilView

问题描述 投票:0回答:1

[我开始从Kivy中学习KIVY – Roberto Ulloa编写的Python交互式应用和游戏-第二版“。我试图通过示例程序来了解collide_point。

在带有以下示例代码的RelativeLayout中,我能够确定何时在水平线上单击鼠标。但是,如果我使用StencilView,则无法捕获触摸事件。

请任何帮助

from kivy.app import App
from kivy.graphics import Line
from kivy.uix.scatter import Scatter
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.stencilview import StencilView


class MyPaintWidget(Scatter):

    def __init__(self, **kwargs) :
        self.selected = None
        self.touched = False
        super(MyPaintWidget, self).__init__(**kwargs)

    def create_figure(self,  **kwargs):
        (ix,iy) = self.to_local(*self.pos)
        (fx,fy) = self.to_local(self.pos[0] + self.size[0], self.pos[1] + self.size[1])
        print ("ix " + str(ix) + " iy " + str(iy) + " fx " + str(fx) + " fy " + str(fy))
        self.canvas.add(Line( points=[ix, iy, fx, fy], width=1))
        return self

    def on_touch_down(self, touch):
        if self.collide_point(touch.x, touch.y):
           #This print I get for RelativeLayout
           #But for StencilView it does not work when I touch the line
           print("Hi you touched the line")

class MyPaintApp(App):

    def build(self):
        #If parent is StencilView unable to click
        #but if parent is RelativeLayout, I am able to get collide_point
        #parent = StencilView()
        parent = RelativeLayout()
        #parent = Scatter()
        ix = 100.
        iy = 100.
        fx = 200.
        fy = 100.
        pos = (min(ix, fx), min(iy, fy))
        size = (abs(fx-ix), abs(fy-iy))

        self.painter = MyPaintWidget(pos=pos, size=size)
        parent.add_widget(self.painter.create_figure())
        return parent

if __name__ == '__main__':
    MyPaintApp().run()
kivy android-relativelayout
1个回答
0
投票
注意
© www.soinside.com 2019 - 2024. All rights reserved.