Kivy 在 canvas 抽奖中有一个可点击的按钮

Kivy have a clickable button on a canvas draw

我正在尝试让人们画一个时钟,当他们画完后点击提交按钮,它就会截屏。当我使用 canvas 绘图时,我不能再使用按钮了。

有没有办法将按钮放在 canvas 的顶部,以便它仍然可以点击?

main.py

class Clock(Screen):
    def on_touch_down(self, touch):
        print(touch)
        with self.canvas.before:
            touch.ud["line"] = Line(points = (touch.x, touch.y), width = 2)

    def on_touch_move(self, touch):
        print(touch)
        touch.ud["line"].points += (touch.x, touch.y)

    def on_touch_up(self, touch):
        print("released", touch)

clock.kv

#:import utils kivy.utils

<Clock>:
    FloatLayout
        canvas.before:
            Color:
                rgba: .5, .5, .5, .5
            Line:
                width: 50
                rectangle: self.x, self.y, self.width, self.height

        Label:
            pos_hint: {"top": .9, "center_x": .5}
            size_hint: 1, .1
            text: "Draw a clock."
            font_size: 25
        BoxLayout:
            id: myexport
        Button:
            pos_hint: {"top": .1, "right": 1}
            size_hint: .1, .1
            text:
                "Submit"
            on_release:
                app.change_screen("animals1")
                myexport.export_to_png("Clock.png")

在您的 Clock class 方法中,您需要调用 super 方法才能正确传播事件:

class Clock(Screen):
    def on_touch_down(self, touch):
        print(touch)
        with self.canvas.before:
            touch.ud["line"] = Line(points = (touch.x, touch.y), width = 2)
        return super(Clock, self).on_touch_down(touch)

    def on_touch_move(self, touch):
        print(touch)
        touch.ud["line"].points += (touch.x, touch.y)
        return super(Clock, self).on_touch_move(touch)

    def on_touch_up(self, touch):
        print("released", touch)
        return super(Clock, self).on_touch_up(touch)