在固定时间后更新 python kivy 中的线段
Update a line segment in python kivy after a fixed time
我正在尝试创建一条线,并且更新是在固定的时间间隔(比如 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()
my_callback
实际上并没有在 with
语句中被调用,而是在 5 秒后当它已经消失时,即使它不会做你想要的 - 它会画一个新行,不修改现有行。
相反,您可以将 my_callback 更改为:
self.line.points = [100, 100, 200, 100, 100, 200]
self.line.width = 10
这将根据需要修改现有行。您也可以从 with 语句中取出时钟调度,但只要它停留在 __init__
中,它的位置实际上并不重要
我正在尝试创建一条线,并且更新是在固定的时间间隔(比如 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()
my_callback
实际上并没有在 with
语句中被调用,而是在 5 秒后当它已经消失时,即使它不会做你想要的 - 它会画一个新行,不修改现有行。
相反,您可以将 my_callback 更改为:
self.line.points = [100, 100, 200, 100, 100, 200]
self.line.width = 10
这将根据需要修改现有行。您也可以从 with 语句中取出时钟调度,但只要它停留在 __init__