Kivy:如何用操作条控制实时图形



我有一个可以实时更新和添加点的图,但我希望能够通过ActionBar(即播放、暂停、停止等)来控制它。到目前为止,无论我尝试什么,它都没有起作用。我需要将图形转换为某种动画吗?

下面是我的.py文件

    class Generator(object):
        def __init__(self, period):
            self.period = period
            self.i = 0
        def __call__(self):
            result = self.i%self.period + sin(self.i)+1
            self.i += 1
            return result
    class GraphView(BoxLayout):
        def __init__(self, **kwargs):
            super(GraphView, self).__init__(**kwargs)
            graph_theme = {'label_options': {'color': rgb('444444'),
                                             'bold': True},
                           'background_color': rgb('f8f8f2'),
                           'tick_color': rgb('808080'),
                           'border_color': rgb('808080')}
            self.graph = Graph(xlabel='Times',
                               ylabel='Sales',
                               x_ticks_minor=5,
                               x_ticks_major=10,
                               y_ticks_minor=5,
                               y_ticks_major=10,
                               y_grid_label=True,
                               x_grid_label=True,
                               padding=5,
                               xlog=False,
                               ylog=False,
                               x_grid=True,
                               y_grid=True,
                               **graph_theme)
            self.input_plot = MeshLinePlot(color=[0, 1, 0, 1])
            self.prediction_plot = MeshLinePlot(color=[1, 0, 0, 1])
            self.input_plotpoints = deque(maxlen=100)
            self.predictions_plotpoints = deque(maxlen=100)
            self.graph.add_plot(self.input_plot)
            self.graph.add_plot(self.prediction_plot)
            for i in xrange(10):
                self.wave1 = Generator(24)
                self.wave2 = Generator(13)
                Clock.schedule_interval(self.update_points, 1 / 100)
                Clock.schedule_interval(self.update_graph, 1 / 100)
                Clock.schedule_interval(lambda *args: self.add_points(self.wave1(), self.wave2()), 0.2)
            self.add_widget(self.graph)
        def update_points(self, *args):
            self.input_plot.points = zip(range(len(self.input_plotpoints)), self.input_plotpoints)
            self.prediction_plot.points = zip(range(len(self.predictions_plotpoints)), self.predictions_plotpoints)
        def update_graph(self, *args):
            if len(self.input_plotpoints) >3:
                self.graph.ymin= math.ceil(min(self.input_plotpoints))
                self.graph.ymax= math.ceil(max(self.input_plotpoints))
        def add_points(self,active,predictive):
            self.input_plotpoints.append(active)
            self.predictions_plotpoints.append(predictive)
        def pause():
            time.sleep(5)      
class ActionView(BoxLayout):
    pass

下面是.kv文件:

<ActionView>:
    use_separator: True
    ActionPrevious:
        title: 'Action Bar'
        with_previous: False
    ActionOverflow:
    ActionButton:
        text: 'Btn0'
        icon: 'atlas://data/images/defaulttheme/audio-volume-high'
    ActionButton:
        text: 'Start'
    ActionButton:
        text: 'Pause'
        on_release:root.GraphView.pause
    ActionButton:
        text: 'Resume'
    ActionButton:
        text: 'Stop'
    ActionGroup:
        text: 'Group1'
        ActionButton:
            text: 'Btn5'
        ActionButton:
            text: 'Btn6'
        ActionButton:
            text: 'Btn7'

好的,答案比预期的要容易。所需要的只是一个布尔值(即pause=False)和更新点和添加点函数中的一些if条件,以检查pause是否为True。

创建一个单独的函数,将暂停从默认值false更改为True,并从操作栏(在.kv中)调用该函数