【问题标题】:How to import graph from another class in Kivy?如何从 Kivy 中的另一个类导入图形?
【发布时间】:2016-11-05 17:04:30
【问题描述】:

我想将 NiceGraph 类中的图形导入 .kv 文件,但我不知道该怎么做。

我阅读了文档,但找不到任何关于在课堂上使用类的信息。

这是我的尝试。

class NiceGraph(BoxLayout):
    graph = Graph(xlabel='X', ylabel='Y', x_ticks_minor=5,
    x_ticks_major=25, y_ticks_major=1,
    y_grid_label=True, x_grid_label=True, padding=5,
    x_grid=True, y_grid=True, xmin=-0, xmax=100, ymin=-1, ymax=1)
    plot = MeshLinePlot(color=[1, 0, 0, 1])
    plot.points = [(x, sin(x / 10.)) for x in range(0, 101)]
    graph.add_plot(plot)

class KivyTesting(BoxLayout):
    pass

class KivyTestingApp(App):
    def build(self):
        return KivyTesting()

kivy_testing_app = KivyTestingApp()
kivy_testing_app.run()

还有.kv文件

<KivyTesting>:
    orientation: 'vertical'
    padding: 10
    slider_colors: 0, 0, 0
    canvas.before:
        Color:
            rgb: root.slider_colors
        Rectangle:
            pos: root.pos
            size: root.size

    BoxLayout:
        size_hint_y: 200
        Slider:
            size_hint_x: 2
            max: 1
            value: 0
            on_value: root.slider_colors[0] = self.value
        Slider:
            size_hint_x: 2
            max: 1
            min: 0
            value: 0
            on_value: root.slider_colors[1] = self.value
        Slider:
            size_hint_x: 2
            max: 1
            min: 0
            value: 0
            on_value: root.slider_colors[2] = self.value

    BoxLayout:
        size_hint_y: 600
        NiceGraph:
            graph:

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    尝试使用

    class NiceGraph(BoxLayout):
        global graph
    
        graph = Graph(xlabel='X', ylabel='Y', 
        x_ticks_minor=5,
        x_ticks_major=25, y_ticks_major=1,
        y_grid_label=True, x_grid_label=True, padding=5,
        x_grid=True, y_grid=True, xmin=-0, xmax=100, ymin=-1, ymax=1)
        plot = MeshLinePlot(color=[1, 0, 0, 1])
        plot.points = [(x, sin(x / 10.)) for x in range(0, 101)]
        graph.add_plot(plot) 
    
    class KivyTesting(BoxLayout):
        graph1 = graph
    

    并使用&lt;KivyTesting&gt;: 中的root.graph1 对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 2019-10-18
      • 2018-10-05
      • 1970-01-01
      相关资源
      最近更新 更多