matplotlib Kivy按下按钮更新绘图

4dc9hkyq  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(211)

开始学习Kivy,不能更新按钮上的图表。理想情况下,在未来,数据应该来自外部,并添加到图表中,而无需按任何键。尝试使用时钟。schedule_interval,但它没有工作。

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import matplotlib.pyplot as plt
from kivy.clock import Clock

x=2
y=4
lst=[x,y]
plt.ylabel('some numbers')

class MainApp(App):

    def update(self):
        Clock.schedule_interval(self.plot,1)
        
    def on_press_button(self, instance):
        for i in range(20):
            global y
            global x
            global lst
            x=x*2
            y=y*2
            lst.append(x)
            lst.append(y)
            print(lst)

        return MainApp.build(self)
        print('Button pressed!')
    
    def build(self):
        plt.cla()
        plt.plot(lst)
        plt.ylabel('some numbers')
        main_layout = BoxLayout(orientation='horizontal')
        grid_layout=GridLayout(cols=1,
                               row_force_default=True,
                               row_default_height=100,
                               size_hint=(.2,1)
                               )
        main_layout.add_widget(grid_layout)
        grid_layout.add_widget(Label(text='Hello from Kivy'))

        button1=grid_layout.add_widget(Button(text='Hello 1', on_press=self.on_press_button))

        main_layout.add_widget(FigureCanvasKivyAgg(plt.gcf(), size_hint=(.8,1)))
        print ('build called')
        return main_layout
        
if __name__ == '__main__':
    app = MainApp()
    app.run()
pgvzfuti

pgvzfuti1#

Appbuild()方法是在调用Apprun()方法时自动调用的,build()返回的widget设置为Approot widget,如果直接调用同一个build()方法,不影响Approot widget。
一种方法是创建一个新的方法来完成绘图并返回FigureCanvasKivyAgg的示例。这个新方法可以在build()方法中调用,也可以在on_press_button()方法中调用,然后返回的FigureCanvasKivyAgg可以用来替换原始的FigureCanvasKivyAgg。下面是代码的修改版本,它可以完成此操作:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import matplotlib.pyplot as plt

x = 2
y = 4
lst = [x, y]
plt.ylabel('some numbers')

class MainApp(App):

    def build_plt(self):
        plt.cla()
        plt.plot(lst)
        plt.ylabel('some numbers')
        return FigureCanvasKivyAgg(plt.gcf(), size_hint=(.8,1))

    def on_press_button(self, instance):
        for i in range(20):
            global y
            global x
            global lst
            x = x * 2
            y = y * 2
            lst.append(x)
            lst.append(y)
            print(lst)

        # return MainApp.build(self)
        self.root.remove_widget(self.fcka)  # remove the current plot
        self.fcka = self.build_plt()  # create the updated plot
        self.root.add_widget(self.fcka)  # insert the new plot
        print('Button pressed!')

    def build(self):
        main_layout = BoxLayout(orientation='horizontal')
        grid_layout=GridLayout(cols=1,
                               row_force_default=True,
                               row_default_height=100,
                               size_hint=(.2,1)
                               )
        main_layout.add_widget(grid_layout)
        grid_layout.add_widget(Label(text='Hello from Kivy'))

        button1=grid_layout.add_widget(Button(text='Hello 1', on_press=self.on_press_button))
        self.fcka = self.build_plt()

        main_layout.add_widget(self.fcka)
        print ('build called')
        return main_layout

if __name__ == '__main__':
    app = MainApp()
    app.run()

相关问题