1

我试图通过从窗口的一个点拖动到另一个点来用鼠标画一条线。我还想在拖动时代表这条线。就像在旧的 MS PaintBrush 中画线一样。

我的问题是我只能通过不断删除旧线并向画布添加的顶点指令来实现这一点。但是,我无法更新现有说明。甚至没有添加和删除相同的指令。它必须是 Line 的新实例。通过运行以下代码,您可以看到我想要的结果。如果您尝试使用注释行运行它,它将不再起作用。

from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Line

class MyCanvas(RelativeLayout):
    def on_touch_down(self, touch):
        with self.canvas:
            self.line = Line(points=[touch.x,touch.y,touch.x+1,touch.y+1])
        self.bind(on_touch_move=self.update_line, on_touch_up=self.end_line)
        return True

    def update_line(self, instance, touch):
        self.line.points[2] = touch.x
        self.line.points[3] = touch.y
        self.canvas.remove(self.line)
#        self.canvas.add(self.line) # - this doesn't work
#        self.canvas.ask_update()   # - not even using this
        with self.canvas:
            self.line = Line(points=self.line.points) # this works

    def end_line(self, instance, touch):
        self.unbind(on_touch_move=self.update_line)
        self.unbind(on_touch_up=self.end_line)
        self.line.points[2] = touch.x
        self.line.points[3] = touch.y
        self.canvas.remove(self.line)
#        self.canvas.add(self.line) # - this doesn't work
#        self.canvas.ask_update()   #- not even using this
        self.canvas.add(Line(points=self.line.points))  # this way works

class ExampleApp(App):
    def build(self):
        return MyCanvas()

ExampleApp().run()

我还尝试使用其他问题中建议的Kivy 属性和 Color 指令。它没有用,还有另一个与之相关的问题

4

1 回答 1

1

我正在努力解决同样的问题。我从 kivy/guide/firstwidget 目录中的 6_button.py 示例开始

我发现了一些可行的方法(使用 pop 两次从点中删除最后一个 x,y 对)但我认为这很尴尬,请参阅下面的代码。我希望有人可以告诉我们如何正确“更新”。

基于 6_button.py

from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line


class MyPaintWidget(Widget):

    def on_touch_down(self, touch):
        color = (random(), 1, 1)
        with self.canvas:
            Color(*color, mode='hsv')
            d = 10.
            Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
            touch.ud['line'] = Line(points=(touch.x, touch.y, touch.x+30, touch.y))
            #print(dir(touch.ud['line']))

    def on_touch_move(self, touch):
        #touch.ud['line'].points += [touch.x, touch.y]
        touch.ud['line'].points.pop()                     #
        touch.ud['line'].points.pop()                     # works but is awkward
        touch.ud['line'].points += [touch.x, touch.y]     #

        #touch.ud['line'].points[2:4] = [touch.x, touch.y] 
        #self.canvas.ask_update()              # no error but didnt work
        #touch.ud['line'].ask_update()         # didnt work
        #print(touch.ud['line'].points)
        #touch.ud['line'].needs_redraw()       # error 'bool not callable'
        #touch.ud['line'].needs_redraw = True  # error 'not writable'
        #touch.ud['line'].needs_redraw         #no error but doesnt work


class MyPaintApp(App):

    def build(self):
        parent = Widget()
        painter = MyPaintWidget()
        clearbtn = Button(text='Clear')
        parent.add_widget(painter)
        parent.add_widget(clearbtn)

        def clear_canvas(obj):
            painter.canvas.clear()
        clearbtn.bind(on_release=clear_canvas)

        return parent


if __name__ == '__main__':
    MyPaintApp().run()
于 2013-08-21T12:25:25.660 回答