由不滚动的 maplotlib 图组成的 kivy scrollview

我目前正在构建一个数字规划器应用程序,我想在其中包含一些与统计相关的功能。显然,matplotlib 是实现此目的的最佳方法,但是当我尝试在 aa Kivy ScrollView 中添加多个图时,我遇到了两个问题:

  1. 每个图的大小都减小了很多,以至于您看不到实际显示的内容;

  2. Kivy ScrollView 不滚动——不幸的是,这很常见。

我已经尝试将 ScrollView 的高度设置为 ScrollView.minimum_height,但我没有得到任何结果。

这是我的一些 Python 代码:

class StatsWindow(FloatLayout, MDTabsBase):

    dates_from, dates_to, plot_scroll  = [str(datetime.today()).split()[0]], [str(datetime.today()).split()[0]], ObjectProperty(None)


    def add_sp_plot(self, date_from, date_to):


        # There were too many lines of data handling and plot creating, so I've only left the KivyPart:

        # ------- KIVY part ------- KIVY part ------- KIVY part ------- KIVY part ------- #


        self.plot_scroll.plot_layout.clear_widgets()

        self.plot_scroll.plot_layout.add_widget(FigureCanvasKivyAgg(plt.gcf(), size_hint_y=None))

        self.plot_scroll.plot_layout.add_widget(FigureCanvasKivyAgg(plt.gcf(), size_hint_y=None))

        self.plot_scroll.plot_layout.add_widget(FigureCanvasKivyAgg(plt.gcf(), size_hint_y=None))


这是我的一些 Kivy 代码:

<StatsWindow>:

    name: "stats"

    text: "STATS"

    icon: "thumbs-up-down"


    plot_scroll: plot_scroll

    choose_date_to: choose_date_to

    choose_date_from: choose_date_from


    FloatLayout:


        MDLabel:

            halign: "center"

            size_hint: 1, .1

            text: "Choose the dates to view your stats"

            font_size: self.width / 17

            color: app.theme_cls.primary_color

            pos_hint: {"top": .98, "center_x": .5}


        BoxLayout:


            MDFlatButton:

                id: choose_date_from


                pos_hint: {"center_x": .25, "top": .88}


                text: "from"

                size_hint: .4, .1

                font_size: self.width / 11

                on_release: root.open_date_picker(root.dates_from, self)


            MDFlatButton:

                text: "|"

                size_hint: .1, .1

                pos_hint: {"center_x": .5, "top": .88}


            MDFlatButton:

                id: choose_date_to


这是我运行程序时得到的图片:


holdtom
浏览 137回答 1
1回答

开满天机

ScrollView仅当其子项 (the GridLayout) 大于 时才会滚动ScrollView。此外,该行:height: self.minimum_height除非您添加以下行,否则将无效:size_hint_y: Nonerow_default_height您可以通过为指定 aGridLayout并为 消除 来size_hint_y=None增加绘图的大小FigureCanvasKivyAgg。因此,我建议将您指定ScrollView为:&nbsp; &nbsp; ScrollView:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; id: plot_scroll&nbsp; &nbsp; &nbsp; &nbsp; do_scroll_y: True&nbsp; &nbsp; &nbsp; &nbsp; do_scroll_x: False&nbsp; &nbsp; &nbsp; &nbsp; pos_hint: {"top": .68}&nbsp; &nbsp; &nbsp; &nbsp; plot_layout: plot_layout&nbsp; &nbsp; &nbsp; &nbsp; GridLayout:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: plot_layout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cols: 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size_hint_y: None&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row_default_height: 500&nbsp; # any default row height that you desire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: self.minimum_height然后,使用以下方法添加绘图:def add_sp_plot(self, date_from, date_to):&nbsp; &nbsp; # There were too many lines of data handling and plot creating, so I've only left the KivyPart:&nbsp; &nbsp; # ------- KIVY part ------- KIVY part ------- KIVY part ------- KIVY part ------- #&nbsp; &nbsp; self.plot_scroll.plot_layout.clear_widgets()&nbsp; &nbsp; self.plot_scroll.plot_layout.add_widget(FigureCanvasKivyAgg(plt.gcf()))&nbsp; &nbsp; self.plot_scroll.plot_layout.add_widget(FigureCanvasKivyAgg(plt.gcf()))&nbsp; &nbsp; self.plot_scroll.plot_layout.add_widget(FigureCanvasKivyAgg(plt.gcf()))删除size_hint_y=None保留默认值size_hint_yas 1,以便绘图将占用GridLayout分配给它的所有空间 (the row_default_height)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python