如何在Kivy kv文件中创建Matplotlib小部件? [英] How do you create a Matplotlib widget in Kivy kv file?
本文介绍了如何在Kivy kv文件中创建Matplotlib小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用.kv文件制作Matplotlib小部件,但是我不确定如何执行此操作.
I want to use a .kv file to make a Matplotlib widget, but I'm not sure how to do this.
没有.kv文件,基本代码如下:
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()
如何使用.kv文件完成此操作?
How is this done with a .kv file?
推荐答案
所以这就是我的想法.在.KV语言文件中,您指定一个布局并为其指定ID:
So here's what I figured out. In the .KV language file you specify a layout and give it an id:
BoxLayout:
id: destination
然后在您的python代码中使用以下代码:
Then in your python code you use the following:
self.ids.destination.add_widget(FigureCanvasKivyAgg(plt.gcf()))
因此,实际上,您可以将在kivy语言文件中设置的ID用作matplotlib图的参考.
So effectively you're using the id you setup in the kivy language file as a reference for your matplotlib graph.
这篇关于如何在Kivy kv文件中创建Matplotlib小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文