简体   繁体   English

在kivy打开MDCard里面的matplotlib

[英]Opening matplotlib inside MDCard in kivy

I've recently started working with Kivy and have been struggling with opening matplotlib graph inside the MDCard dedicated for it.我最近开始使用 Kivy 并一直在努力打开专用于它的 MDCard 中的 matplotlib 图。 All the available codes run solely the matplotlib graph class inside the App class but I want to access it from the second screen that I enter into and then by pressing the MDCard meant for it.所有可用的代码仅运行 App class 内的 matplotlib 图表 class 但我想从我进入的第二个屏幕访问它,然后按下 MDCard 意味着它。 Here, when I'm pressing the button nothing is showing.在这里,当我按下按钮时,没有任何显示。 Here's my code.这是我的代码。 Kindly let me know what should I be doing:请让我知道我应该做什么:

project.py:项目.py:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.card import MDCard
from kivymd.uix.boxlayout import MDBoxLayout
import matplotlib.pyplot as plt
import numpy as np
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import webbrowser

Window.clearcolor = (1, 1, 1, 1)
Window.size = (360, 600)

class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):
    pass

x = [1,2,3,4,5]
y = [5, 12, 6, 9, 15]

signal_x = np.array(x)
signal_y = np.array(y)

plt.plot(signal_x,signal_y)

plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")

plt.grid(True, color='lightgray')

class ScreenThree(Screen):
    def graph(self):
        app = MDApp.get_running_app()
        box = MDBoxLayout()
        box.add_widget(FigureCanvasKivyAgg(plt.gcf()))
        return box


sm = ScreenManager()
sm.add_widget(ScreenOne(name='screen1'))
sm.add_widget(ScreenTwo(name='screen2'))
sm.add_widget(ScreenThree(name='screen3'))


class KivyApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Cyan"
        self.theme_cls.primary_hue = "A700"
        self.theme_cls.theme_style = "Light"
        return Builder.load_file('project.kv')
    
    def url_Function(self):
        webbrowser.open('http://www.google.com')
    

    


KivyApp().run()

project.kv:项目.kv:

ScreenManager:
    ScreenOne:
    ScreenTwo:
    ScreenThree:

<ScreenOne>:
    name: 'screen1'
    MDFillRoundFlatButton:
        text: 'Get Started'
        pos_hint: {'center_x':0.5,'center_y':0.4}
        on_press: root.manager.current = 'screen2'

<ScreenTwo>:
    name: 'screen2'
    ScrollView:
        size: self.size
        GridLayout:
            size_hint_y: None
            height: self.minimum_height
            width: self.minimum_width
            cols: 1
            spacing: "20dp"
            padding: "20dp"

            MDCard:
                orientation: "vertical"
                padding: "8dp"
                size_hint: 1, None
                height: "210dp"
                elevation: 5
                border_radius: 10
                radius: [15]
                ripple_behavior: True
                on_press: app.url_Function()

                MDLabel:
                    text: 'Link'
                    bold: True
                    color: (64/255, 75/255, 122/255, 1)
                    font_size: 20
                    halign: 'center'

            MDCard:
                orientation: "vertical"
                padding: "8dp"
                size_hint: 1, None
                height: "210dp"
                elevation: 5
                border_radius: 10
                radius: [15]
                ripple_behavior: True
                on_press: root.manager.current = 'screen3'

                MDLabel:
                    text: 'Plot'
                    bold: True
                    color: (64/255, 75/255, 122/255, 1)
                    font_size: 20
                    halign: 'center'
                    valign: 'middle'

<ScreenThree>
    name: 'screen3'
    id: _box
    Button:
        text: 'Do it'
        on_release: _box.graph()

You are missing a box, which shall contain the FigureCanvasKivyAgg object.您缺少一个框,其中应包含FigureCanvasKivyAgg object。

project.py项目.py

class ScreenThree(Screen):
    def graph(self):
        self.ids.box_plot.add_widget(FigureCanvasKivyAgg(plt.gcf()))
        plt.close()

project.kv项目.kv

<ScreenThree>:
    name: 'screen3'
    id: _box
    Button:
        text: 'Do it'
        on_release: _box.graph()
    MDBoxLayout:
        id: box_plot

This will show the plot after you click on the "Do it"-button of your 3rd screen.单击第三个屏幕的“执行”按钮后,这将显示 plot。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM