Kivy 2.3.0:AttributeError:'FigureCanvasKivyAgg'对象没有属性'resize_event'

问题描述 投票:0回答:1

我试图在我的 Kivy 应用程序中创建一个基本图表,但在使用 FigureCanvasKivyAgg(plt.gcf()) 来绘制它时遇到困难,但我不断收到 AttributeError: 'FigureCanvasKivyAgg' object has no attribute 'resize_event'。尽管进行了广泛的搜索并观看了教程,但我还没有找到解决此问题的任何方法。看来其他人可以使用FigureCanvasKivyAgg成功绘图,但我使用的是Kivy 2.3.0,并且我一直无法找到解决我的特定问题的文档。

这是我用来向 Kivy 应用程序添加绘图的示例应用程序

马蒂.py

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.metrics import dp, sp
from kivy.clock import Clock


#Graphical Imports
from kivymd.app import MDApp
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import matplotlib.pyplot as plt

x = [ 1, 2, 3, 4, 5]
y = [5, 14, 7, 20, 11]

plt.plot(x,y)
plt.ylabel("y axis")
plt.xlabel("x axis")

class WelcomeScreen(Screen):
    pass

class FirstScreen(Screen):
    pass

class SecondScreen(Screen):
    pass

class ScreenManager(ScreenManager):
    pass

class Graph(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_widget(FigureCanvasKivyAgg(plt.gcf()))

    def save(self):
        pass

class Project(BoxLayout):
    pass

kv = Builder.load_file("matty.kv")

class TestApp(MDApp):
    title = "Kivy Project"

    def build(self, **kwargs):
        return Project()
   
if __name__ == '__main__':
    TestApp().run()

马蒂.kv

#:kivy 2.3.0
<Project>:
    orientation: "vertical"

    canvas.before:
        Color:
            rgb: .6, .6, .6
        Rectangle:
            pos: self.pos
            size: self.size
   
    SomeMenu_ActionBar:
        id: ActionBar

    ScreenManager:
        id: sm
        WelcomeScreen:
        FirstScreen:
        SecondScreen:
            Graph:
                id: graph_screen

<SomeMenu_ActionBar@ActionBar>:

    ActionView:
        id: ActionView

        HiddenIcon_ActionPrevious:

        ActionGroup:
            id: App_ActionGroup
            mode: "spinner"
            text: "Jump to Screen"

            ActionButton:
                text: "Welcome Screen"
                on_release:
                    app.root.ids.sm.current = 'welcome'
                    app.root.ids.sm.transition.direction = "right"
            ActionButton:
                text: "First Screen"
                on_release:
                    app.root.ids.sm.current = 'first'
                    app.root.ids.sm.transition.direction = "left"
            ActionButton:
                text: "Second Screen"
                on_release:
                    app.root.ids.sm.current = 'second'
                    app.root.ids.sm.transition.direction = "left"
       
        ActionGroup:
            id: App_ActionGroup
            mode: 'spinner'
            text: 'App'

            ActionButton:
                text: 'Settings'
                on_press: app.open_settings()
            ActionButton:
                text: 'Quit'
                on_press: app.get_running_app().stop()
       
        ActionGroup:
            id: File_ActionGroup
            mode: 'spinner'
            text: 'File'

            ActionButton:
                text: 'Open'
            ActionButton:
                text: 'Save'
       
        ActionButton:
            text: 'Back to Main Menu'
            on_release:
                app.root.ids.sm.current = 'welcome'
                app.root.ids.sm.transition.direction = "right"

<HiddenIcon_ActionPrevious@ActionPrevious>:
    title: ''
    with_previous: False
    app_icon: ''
    app_icon_width: 0
    app_icon_height: 0
    size_hint_x: None
    width: len(self.title)*10

<WelcomeScreen>:
    name: 'welcome'
    Label:
        text: 'Welcome Screen'
        font_size: sp(50)

<FirstScreen>:
    id: first
    name: 'first'
    Label:
        text: 'First Screen'

<SecondScreen>:
    id: second
    name: 'second'
    Graph:
        id: graph_screen
<Graph>:
    id: boxgraph
    BoxLayout:
        size_hint_y: .9
        pos_hint: {"top": 1}
       
    BoxLayout:
        size_hint_y: .1
        TextInput:
            id: name
            multiline: False

        Button:
            text: "Save"
            on_release: root.save()

错误

 Traceback (most recent call last):
   File "c:\Users\alisu\OneDrive - UWE Bristol\UWE Bristol & Stuff\3rd Year UWE\Digital Systems\Alistana Fitness & Nutrition Tracker\AFNT-Digital-Systems-Project\AFNT\Application\Classes\matty.py", line 58, in <module>
     TestApp().run()
   File "C:\Users\alisu\OneDrive - UWE Bristol\UWE Bristol & Stuff\3rd Year UWE\Digital Systems\Alistana Fitness & Nutrition Tracker\AFNT-Digital-Systems-Project\AFNT\.venv\Lib\site-packages\kivy\app.py", line 956, in run
     runTouchApp()
   File "C:\Users\alisu\OneDrive - UWE Bristol\UWE Bristol & Stuff\3rd Year UWE\Digital Systems\Alistana Fitness & Nutrition Tracker\AFNT-Digital-Systems-Project\AFNT\.venv\Lib\site-packages\kivy\base.py", line 574, in runTouchApp
     EventLoop.mainloop()
   File "C:\Users\alisu\OneDrive - UWE Bristol\UWE Bristol & Stuff\3rd Year UWE\Digital Systems\Alistana Fitness & Nutrition Tracker\AFNT-Digital-Systems-Project\AFNT\.venv\Lib\site-packages\kivy\base.py", line 339, in mainloop
     self.idle()
   File "C:\Users\alisu\OneDrive - UWE Bristol\UWE Bristol & Stuff\3rd Year UWE\Digital Systems\Alistana Fitness & Nutrition Tracker\AFNT-Digital-Systems-Project\AFNT\.venv\Lib\site-packages\kivy\base.py", line 379, in idle
     Clock.tick()
   File "C:\Users\alisu\OneDrive - UWE Bristol\UWE Bristol & Stuff\3rd Year UWE\Digital Systems\Alistana Fitness & Nutrition Tracker\AFNT-Digital-Systems-Project\AFNT\.venv\Lib\site-packages\kivy\clock.py", line 733, in tick
     self.post_idle(ts, self.idle())
   File "C:\Users\alisu\OneDrive - UWE Bristol\UWE Bristol & Stuff\3rd Year UWE\Digital Systems\Alistana Fitness & Nutrition Tracker\AFNT-Digital-Systems-Project\AFNT\.venv\Lib\site-packages\kivy\clock.py", line 776, in post_idle
     self._process_events()
   File "kivy\\_clock.pyx", line 620, in kivy._clock.CyClockBase._process_events
   File "kivy\\_clock.pyx", line 653, in kivy._clock.CyClockBase._process_events
   File "kivy\\_clock.pyx", line 649, in kivy._clock.CyClockBase._process_events
   File "kivy\\_clock.pyx", line 218, in kivy._clock.ClockEvent.tick
   File "C:\Users\alisu\OneDrive - UWE Bristol\UWE Bristol & Stuff\3rd Year UWE\Digital Systems\Alistana Fitness & Nutrition Tracker\AFNT-Digital-Systems-Project\AFNT\.venv\Lib\site-packages\kivy\uix\floatlayout.py", line 97, in do_layout
     c.size = c_w, c_h
     ^^^^^^
   File "kivy\\properties.pyx", line 520, in kivy.properties.Property.__set__
   File "kivy\\properties.pyx", line 1478, in kivy.properties.ReferenceListProperty.set
   File "kivy\\properties.pyx", line 606, in kivy.properties.Property._dispatch
   File "kivy\\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
   File "kivy\\_event.pyx", line 1213, in kivy._event.EventObservers._dispatch
   File "C:\Users\alisu\OneDrive - UWE Bristol\UWE Bristol & Stuff\3rd Year UWE\Digital Systems\Alistana Fitness & Nutrition Tracker\AFNT-Digital-Systems-Project\AFNT\.venv\Lib\site-packages\kivy\garden\matplotlib\backend_kivy.py", line 1233, in _on_size_changed
     self.resize_event()
     ^^^^^^^^^^^^^^^^^
 AttributeError: 'FigureCanvasKivyAgg' object has no attribute 'resize_event'
python-3.x kivy kivy-language kivymd
1个回答
0
投票

这是最近修复的一个错误,但您需要安装更新的 kivy Garden matplotlib,如下所示:

python -m pip install https://github.com/kivy-garden/matplotlib/archive/master.zip

请参阅 github 源代码

您需要将导入从

kivy.garden
更改为
kivy_garden

© www.soinside.com 2019 - 2024. All rights reserved.