【问题标题】:Kivy have a clickable button on a canvas drawKivy 在画布绘制上有一个可点击的按钮
【发布时间】:2019-07-24 16:38:05
【问题描述】:

我试图让人们画一个时钟,当他们完成点击提交按钮时,它会截取屏幕截图。当我使用画布绘制时,我不能再使用按钮了。

有没有办法将按钮放置在画布顶部以使其仍可点击?

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)

时钟.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")

【问题讨论】:

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


    【解决方案1】:

    在您的Clock 类方法中,您需要调用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)
    

    【讨论】:

      猜你喜欢
      • 2013-01-07
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      相关资源
      最近更新 更多