在固定时间后更新python kivy中的线段

时间:2015-03-12 15:53:31

标签: python kivy

我正在尝试创建一条线,并且在固定的时间间隔(例如5秒)之后进行更新。我编写了下面的代码,但它没有更新该行。任何人都可以帮我弄清楚该做什么吗?

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.graphics import Color, Ellipse, Line
import time
from kivy.clock import Clock

class MyWidget(Widget):
    def my_callback(dt,ds):
        Line(points=[100, 100, 200, 100, 100, 200], width=10)
        pass

    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
        with self.canvas:
            self.line = Line(points=[100, 100, 200, 100, 100, 200], width=1)
            self.line.width = 2
            Clock.schedule_once(self.my_callback, 5)
            pass
            # add your instruction for main canvas here

        with self.canvas.before:
            pass
            # you can use this to add instructions rendered before

        with self.canvas.after:
            pass
            # you can use this to add instructions rendered after

class LineExtendedApp(App):
    def build(self):
        root = GridLayout(cols=2, padding=50, spacing=50)
        root.add_widget(MyWidget())
        return root

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

1 个答案:

答案 0 :(得分:0)

my_callback实际上并没有在with语句中被调用,而是在5s之后很久就被调用了,即使它不能做你想做的事情 - 它会画出一个新行,而不是修改现有的。

相反,您可以将my_callback更改为:

self.line.points = [100, 100, 200, 100, 100, 200]
self.line.width = 10

这将根据需要修改现有行。你也可以在with语句之外进行时钟安排,但只要它保持在__init__

,它的位置实际上并不重要。