How do you create a Matplotlib widget in Kivy kv file?

百般思念 提交于 2019-12-11 17:13:14

问题


I want to use a .kv file to make a Matplotlib widget, but I'm not sure how to do this.

Without a .kv file, the basic code looks like this:

from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import matplotlib.pyplot as plt

plt.plot([1, 23, 2, 4])
plt.ylabel('some numbers')

class MyApp(App):

    def build(self):
        box = BoxLayout()
        box.add_widget(FigureCanvasKivyAgg(plt.gcf()))
        return box

MyApp().run()

How is this done with a .kv file?


回答1:


So here's what I figured out. In the .KV language file you specify a layout and give it an id:

BoxLayout:
    id: destination

Then in your python code you use the following:

self.ids.destination.add_widget(FigureCanvasKivyAgg(plt.gcf()))

So effectively you're using the id you setup in the kivy language file as a reference for your matplotlib graph.



来源:https://stackoverflow.com/questions/50012762/how-do-you-create-a-matplotlib-widget-in-kivy-kv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!