如何使猕猴桃只单击顶部的小部件

时间:2019-03-20 00:18:29

标签: python-3.x kivy kivy-language

我要创建它,因此单击此窗口小部件时不会单击它后面的窗口小部件,而仍然可以单击该父窗口小部件的子窗口小部件。每当我添加一些东西来终止事件时,单击它就不会在“内容”窗口小部件的子项上注册点击。如果我没有鼠标,那么它会在鼠标被覆盖或没有覆盖的所有内容上单击。这是我制作的可调整大小的子窗口的代码:

class SubWindow(Widget):
tPosRel = ListProperty()
bColor = ListProperty()
innerColor = ListProperty()
buttonPos = ListProperty()
innerPos = ListProperty()
innerSize = ListProperty()

def __init__(self, **kwargs):
    self.innerPos = (self.pos[0] + 5, self.pos[1] + 40)
    self.innerSize = (90, 265)
    self.innerColor = (.2, .2, .2, 1)
    self.bColor = (0, 0, 0, 1)
    brightnessAvg = (self.bColor[0] + self.bColor[1] + self.bColor[2])/3.0
    if brightnessAvg > .5:
        self.innerColor = (kwargs["bColor"][0] - .2, kwargs["bColor"][1] - .2, kwargs["bColor"][2] - .2, 1)
    else:
        self.innerColor = (kwargs["bColor"][0] + .2, kwargs["bColor"][1] + .2, kwargs["bColor"][2] + .2, 1)
    #print(self.innerColor)
    self.buttonPos = (self.size[0] - 30, self.size[1] - 30)
    #print(self.pos)
    super().__init__(**kwargs)
    self.innerPos = (self.pos[0] + 5, self.pos[1] + 5)
    #print(self.pos)
    #print(self.bColor)
    #print(self.size)
    self.buttonPos = (self.pos[0] + self.size[0] - 25, self.pos[1] + self.size[1] - 25)

def on_pos(self, *args):
    self.innerPos = (self.pos[0] + 5, self.pos[1] + 5)
    self.buttonPos = (self.pos[0] + self.size[0] - 25, self.pos[1] + self.size[1] - 25)
    if self.ready:
        for x in self.ids["content"].children:
            x.pos = self.ids["content"].pos
    #print(self.innerColor)

def on_size(self, *args):
    self.innerSize = (self.size[0] - 10, self.size[1] - 45)
    self.buttonPos = (self.pos[0] + self.size[0] - 25, self.pos[1] + self.size[1] - 25)

def on_touch_down(self, touch):
    if not self.collide_point(*touch.pos):
        return super(SubWindow, self).on_touch_down(touch)

    # print(self.size)
    # print("Mouse X: " + str(touch.x))
    # print("Mouse Y: " + str(touch.y))
    if touch.x > self.pos[0] and touch.x < self.pos[0] + self.size[0] - 30 and touch.y > self.pos[1] + self.size[
        1] - 5 and touch.y < self.pos[1] + self.size[1] + 5:
        self.side = 'up'
        print("up hit detect...")
        self.yInit = self.pos[1]
        self.hInit = self.size[1]
        # self.tPosRel = touch - self.pos
        touch.grab(self)
        return True

    if touch.x > self.pos[0] + self.size[0] - 5 and touch.x < self.pos[0] + self.size[0] + 5 and touch.y > self.pos[
        1] + 5 and touch.y < self.pos[1] + self.size[1] - 21:
        self.side = 'right'
        print("right hit detect...")
        self.xInit = self.pos[0]
        self.wInit = self.size[0]
        touch.grab(self)
        return True

    if touch.x > self.pos[0] + 5 and touch.x < self.pos[0] + self.size[0] - 5 and touch.y > self.pos[
        1] - 5 and touch.y < self.pos[1] + 5:
        self.side = 'down'
        print("down hit detect...")
        self.yInit = self.pos[1]
        self.hInit = self.size[1]
        touch.grab(self)
        return True

    if touch.x > self.pos[0] - 5 and touch.x < self.pos[0] + 5 and touch.y > self.pos[1] + 5 and touch.y < self.pos[
        1] + self.size[1]:
        self.side = 'left'
        print("left hit detect...")
        #print(self.size)
        self.xInit = self.pos[0]
        self.wInit = self.size[0]
        touch.grab(self)
        return True

    if touch.x > self.pos[0] and touch.x < self.pos[0] + self.size[0] - 30 and touch.y > self.pos[1] + self.size[
        1] - 22 and touch.y < self.pos[1] + self.size[1] - 5:
        print("top hit detect...")
        self.side = 'window'
        self.tPosRel = (touch.x - self.pos[0], touch.y - self.pos[1])
        touch.grab(self)
        return True

    if touch.x > self.pos[0] - 5 and touch.x < self.pos[0] + 5 and touch.y > self.pos[1] - 5 and touch.y < self.pos[
        1] + 5:
        self.side = 'leftcorner'
        print("left hit detect...")
        #print(self.size)
        self.xInit = self.pos[0]
        self.wInit = self.size[0]
        self.yInit = self.pos[1]
        self.hInit = self.size[1]
        touch.grab(self)
        return True

    if touch.x > self.pos[0] + self.size[0] - 5 and touch.x < self.pos[0] + self.size[0] + 5 and touch.y > self.pos[
        1] - 5 and touch.y < self.pos[1] + 5:
        self.side = 'rightcorner'
        print("right hit detect...")
        #print(self.size)
        self.xInit = self.pos[0]
        self.wInit = self.size[0]
        self.yInit = self.pos[1]
        self.hInit = self.size[1]
        touch.grab(self)
        return True
    return super(SubWindow, self).on_touch_down(touch)

Kv语言代码:

<SubWindow>:
size_hint: (None, None)
FloatLayout:
    size: root.size
    pos: root.pos
    size_hint: (None, None)
    canvas:
        Color:
            rgba: root.bColor
        Rectangle:
            size: root.size
            pos: root.pos
    Button:
        size: 20, 20
        pos: root.buttonPos
        size_hint: (None, None)
        text: "X"
        on_release: root.close(root)
    FloatLayout:
        pos: root.innerPos
        size: root.innerSize
        size_hint:(None, None)
        id: content
        canvas:
            Color:
                rgba: root.innerColor
            Rectangle:
                pos: content.pos
                size: root.innerSize

1 个答案:

答案 0 :(得分:0)

因此,我在此末尾添加了一段代码。我做到了,所以它只将on_touch_down事件调度到子窗口的子级。

    if self.ids["content"].collide_point(*touch.pos):
        for x in self.ids["content"].walk():
            x.on_touch_down(touch)
        return True