【发布时间】:2013-06-12 09:29:47
【问题描述】:
我试图用鼠标从窗口的一个点拖动到另一个点来画一条线。我还想在拖动时代表这条线。就像在旧的 MS PaintBrush 中画线一样。
我的问题是我只能通过不断删除旧线并在画布上添加 new 顶点指令来实现这一点。但是,我无法更新现有说明。甚至没有添加和删除相同的指令。它必须是 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()
我还尝试使用带有颜色指令的 in this other question 建议的 Kivy 属性。没用,还有another question related to it。
【问题讨论】:
-
我用
ipdb深入研究了代码,我意识到有一个名为needs_redraw 的Line'attribute。在当前行中始终为 False,并且变量被写保护。
标签: kivy