【问题标题】:Add button and matplotlib to boxlayout将按钮和 matplotlib 添加到 boxlayout
【发布时间】:2020-08-01 06:29:00
【问题描述】:

在 kivy 中,我打开了一个模态视图,在该模态视图中是一个 Boxlayout,它将有 2 个小部件。 1 个小部件可以是任何东西,另一个是 matplotlib 图 - 所以我可以同时看到两者。

我无法让 modalview 与 matplotlib 一起工作,当我运行它时,它会取消 modalview 并只显示 matplotlib - 再一次,整个 modalview 只是没有出现,它只是情节。我尝试用有限的 size_hint 制作另一个 kivy 小部件,然后将绘图添加到其中,然后将小部件添加到我的主 boxlayout 但结果相同

我已经附上了我的 kivy 代码和情节代码,图像是我希望模态视图看起来的样子,右侧是情节

最后,我确信还有其他方法可以做我想做的事,比如 matplotlib 有我可以使用的小部件。但这似乎是一种不好的做法,突然从我一直使用的 kivy 转变为我打赌可以以某种方式做到这一点

有什么建议吗?

   mainview = ModalView(size_hint = (0.6, 0.7))
   box = BoxLayout(orientation = 'horizontal')   # cols not rows

   box.add_widget(Button(text = "place holder", size_hint_x = 0.3))
   box.add_widget(python_testing.make_plot())

   mainview.add_widget(box)
        
   mainview.open()
   # this is python_testing.make_plot() mentioned above
   # Prepare the data
   x = [1,2,3,4,5,6,7,8,9,10]
   ratings = [4,5,8,9,4,3,2,6,8,7]

   plt.figure(num='cde')
   plt.plot(x, ratings, label='cde')
   plt.legend()   # Add a legend
   plt.show()     # Show the plot

【问题讨论】:

  • 我找到了不同的解决方案。我没有找到使用 matplotlib 的解决方案。查看答案

标签: python matplotlib user-interface kivy


【解决方案1】:

解决方案:我找到了可以将 matplotlib 与 kivy 集成的东西,但文档非常糟糕,但我找到的解决方案更好。

我发现了 kivy garden(用户制作的 kivy 包),它有一个包可以在 kivy 中制作图表。附上我的新代码,以及我的程序截图

代码:用户按下了触发此方法的按钮。它制作了一个 modalview,其中包含一个 boxlayout,其中包含 2 个小部件 - 1 是由标签和按钮组成的可滚动网格布局,另一个是绘图。该图的代码是代码的第二个sn-p

    # pm: ticker = ticker clicked
    def modal_view(self, ticker, instance):
        mainview = ModalView(size_hint = (0.75, 0.75))
        box = BoxLayout(orientation = "horizontal")
        
        # make a scrollable gridlayout
        scroll = ScrollView(do_scroll_x = False, do_scroll_y = True, size_hint_x = 0.3)                           # a scroll view will contain the ticker gridlayout
        ticker_grid = GridLayout(rows = len(self.ticker_list), cols = 2, size_hint_y = None)   # holds tickers
        ticker_grid.bind(minimum_height = ticker_grid.setter("height"))                    # makes the gridlayout scrollabel via the scrollview
        
        # populate the scrollview / gridlayout
        for i in range(0, len(self.ticker_list)):
            ticker_grid.add_widget(Label(text = self.ticker_list[i // 2], height = 200, size_hint_x = 0.6))
            ticker_grid.add_widget(Button(text = "", size_hint_x = 0.4))

            # if ticker is selected, add button to cancel it
            if (self.ticker_list[i] == ticker):
                ticker_grid.children[i].text = 'X'
                ticker_grid.children[i].fbind("on_release", self.modalview_cancel_ticker, ticker_grid.children[i])

        # combine everything
        scroll.add_widget(ticker_grid)                
        box.add_widget(scroll)
        box.add_widget(python_testing.make_graph())
        mainview.add_widget(box)
        
        mainview.open()
def make_graph():
    # Prepare the data
    x = [1,2,3,4,5,6,7,8,9,10]
    ratings = [4,5,8,9,4,3,2,6,8,7]

    # make the graph
    graph = Graph(xlabel='Dates', ylabel='Ratings', x_ticks_minor = 1, x_ticks_major = 2,
                    y_ticks_minor = 1, y_ticks_major = 2, y_grid_label=True, x_grid_label=True,
                    padding=5, x_grid=True, y_grid=True, xmin=0, xmax=10, ymin=0, ymax=10)

    plot = MeshLinePlot(color=[1, 0, 0, 1])
    plot.points = [(i, j) for i, j in zip(x, ratings)]

    graph.add_plot(plot)
    return graph

如果您使用此代码,请注意:在 make_graph() 中,显示数据点很棘手。 plot.points = [(i, j) for i, j in zip(x, ratings)] 是我能做到的唯一方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 2021-12-30
    • 2017-04-09
    • 2017-08-15
    相关资源
    最近更新 更多