[英]Opening matplotlib inside MDCard in kivy
我最近開始使用 Kivy 並一直在努力打開專用於它的 MDCard 中的 matplotlib 圖。 所有可用的代碼僅運行 App class 內的 matplotlib 圖表 class 但我想從我進入的第二個屏幕訪問它,然后按下 MDCard 意味着它。 在這里,當我按下按鈕時,沒有任何顯示。 這是我的代碼。 請讓我知道我應該做什么:
項目.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()
項目.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()
您缺少一個框,其中應包含FigureCanvasKivyAgg
object。
項目.py
class ScreenThree(Screen):
def graph(self):
self.ids.box_plot.add_widget(FigureCanvasKivyAgg(plt.gcf()))
plt.close()
項目.kv
<ScreenThree>:
name: 'screen3'
id: _box
Button:
text: 'Do it'
on_release: _box.graph()
MDBoxLayout:
id: box_plot
單擊第三個屏幕的“執行”按鈕后,這將顯示 plot。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.