更新on_touch_move函数中的小部件

时间:2020-01-04 18:52:08

标签: python kivy

如何在on_touch_move函数中更新小部件?

我了解到on_touch_move功能被激活后,每次都会添加一个新元素,但是我不知道如何更改一个元素的位置

main.py文件

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.image import Image



class Image(Image):
    source = 'pencil.png'
    def __init__(self, x, y):
        super().__init__()
        self.pos = (x, y)


class Wid (Widget):
    def on_touch_down(self, touch):
        self.add_widget(Image(touch.x, touch.y))

    def on_touch_move(self, touch):
        self.add_widget(Image(touch.x, touch.y))
        print(Image(touch.x, touch.y).pos)


class MainApp(App):
    def build(self):
        res = Widget()
        res.add_widget(Wid())
        return res


if __name__ == '__main__':
    MainApp().run()

main.kv文件

#:kivy 1.11.1


<Image>:
    pos_hint:None, None
    pos: self.pos

enter image description here

0 个答案:

没有答案