Python到KV Lang-FigureCanvasKivyAgg [英] Python to KV Lang - FigureCanvasKivyAgg

查看:106
本文介绍了Python到KV Lang-FigureCanvasKivyAgg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用KV Lang在我的代码下编写FigureCanvasKivyAgg吗? 我需要在"on_enter"函数中插入按钮并自定义boxlayout,我认为用KV Lang编写图形会更容易.

Can I write the FigureCanvasKivyAgg under my code, using KV Lang? I need to insert button and customize the boxlayout in the function "on_enter" and I think it would be easier to have the graph written in KV Lang.

我现在遇到的问题是,我试图在"grafico屏幕"中添加一个按钮,尽管该按钮从一开始就出现了,此后,图形在框布局中进行. 我想知道如果我可以用KV Lang编写所有内容,那么具有框布局,按钮等会更容易.

The problem that I am having now, is that I tryied to add a button in the "grafico screen", although the button appears from one moment and after that, the graph takes place in the box layout. I was wondering that if I can write everything in KV Lang it would be easier to have a box layout, button and so on.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("module://kivy.garden.matplotlib.backend_kivy")
from kivy.garden.matplotlib import FigureCanvasKivyAgg
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
import numpy as np


f = 10 #frequency
x = np.linspace(0,1,200)
y = np.sin(2*np.pi*f*x)


class Gerenciador(ScreenManager):
    pass

class Menu(Screen):
    pass

class Sensores(Screen):
    pass

class Grafico(Screen):
    def on_enter(self, *args):
        box = BoxLayout()
        box.add_widget(FigureCanvasKivyAgg(plt.gcf()))
        self.add_widget(box)

class sensor(App):
        def build(self):
            return Gerenciador()

if __name__ == "__main__":
    sensor().run()


    KV LANG

#:import np numpy
#:import FigureCanvasKivyAgg kivy.garden.matplotlib


<Gerenciador>:
    Menu:
        name: 'menu'
    Sensores:   #analogo a nossa tela 01
        name: 'sensores'
    Grafico:
        name: 'grafico'

<Menu>:
    BoxLayout:
        orientation: 'vertical'
        padding: 200
        spacing: 50
        Image:
            source: 'logo.png'
            size_hint_y: None
            height: 200
            allow_stretch: True
        Button:
            text: 'Lista de Sensores'
            on_release: app.root.current = 'sensores'
        Button:
            text: 'Sair'
            on_release: app.stop()

<Sensores>:
    BoxLayout:
        orientation: 'vertical'
        padding: 50
        spacing: 10
        Button:
            text: 'Sensor 01'
            on_release:
                root.manager.current = 'grafico'
        Button:
            text: 'Sensor 02'
        Button:
            text: 'Sensor 03'
        Button:
            text: 'Sensor 04'


<Grafico>:
    BoxLayout:
        Button:
            size_hint: 0.5, 0.09
            pos_hint: {"x": .1 , "y": .2}
            text: "Atualizar"

我需要获得与现在相同的结果,但是要使用KV Lang.

I need to have the same results that I have now, but using KV Lang.

推荐答案

kv中使用的基维小部件必须具有无位置参数__init__()方法. FigureCanvasKivyAgg扩展了Widget,但是具有必需的__init__()参数,该参数是该图,因此您不能直接在kv中使用它(我认为这是一个糟糕的设计选择).

Kivy widgets used in kv must have a no positional arguments __init__() method. The FigureCanvasKivyAgg extends Widget, but has a required __init__() argument, which is the figure, so you cannot use that directly in kv (a poor design choice, in my opinion).

但是您可以创建一个可能对您有用的技巧.以下内容取决于启动sensor应用程序之前是否有可用的图形.您可以将FigureCanvasKivyAgg扩展为:

But you can make a hack that might work for you. The following depends on having a figure available before the sensor app is started. You can extend the FigureCanvasKivyAgg as:

class MyFigure(FigureCanvasKivyAgg):
    def __init__(self, **kwargs):
        super(MyFigure, self).__init__(plt.gcf(), **kwargs)

请注意,MyFigure没有必需的位置参数.但是它使用必需的Figure参数调用FigureCanvasKivyAgg.__init__().这就是为什么该图必须已经可用的原因.

Note that MyFigure has no required positional arguments. But it calls the FigureCanvasKivyAgg.__init__() with the required figure argument. This is why the figure must already be available.

然后您可以在kv文件中将MyFigure用作:

You can then use MyFigure in your kv file as:

<Grafico>:
    BoxLayout:
        MyFigure:
        Button:
            size_hint: 0.5, 0.09
            pos_hint: {"x": .1 , "y": .2}
            text: "Atualizar"

当然,必须删除Grafico类的on_enter()方法.

Of course, the on_enter() method of your Grafico class must be removed.

这篇关于Python到KV Lang-FigureCanvasKivyAgg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆