使用KIVY将图形显示为Box布局的一部分

时间:2018-10-23 16:51:10

标签: python kivy

我是使用Kivy的新手。我想做的是将图形作为方框布局的一部分插入。我收到以下错误消息:AttributeError:'kivy.properties.DictProperty'对象没有属性'destination'

我确定解决方案很简单,但似乎无法实现。任何帮助都会很棒。

Python代码:

import kivy
kivy.require('1.9.0')

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 BoxLayoutApp(App):

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

blApp = BoxLayoutApp()
blApp.run()

Kivy代码============================================== =========================

<BoxLayout>:
    orientation: "vertical"
    spacing: 10
    padding: 10
    canvas.before:
        Rectangle:
            id: destination
            pos: self.pos
            size: self.size
    Button:
        text: "2nd"     
    Button:
        text: "3rd"

1 个答案:

答案 0 :(得分:0)

实际上,我认为ids字典不是您的问题。您似乎正在尝试将FigureCanvasKivyAgg添加到Rectangle画布指令中。您应该将其添加到BoxLayout中,如下所示:

import kivy
from kivy.lang import Builder

kivy.require('1.9.0')

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

Builder.load_string('''
<BoxLayout>:
    orientation: "vertical"
    spacing: 10
    padding: 10
    Button:
        text: "2nd"     
    Button:
        text: "3rd"
''')

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

class BoxLayoutApp(App):

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

blApp = BoxLayoutApp()
blApp.run()