问题
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