如何从Kivy中的另一个类导入图形?

时间:2016-11-05 17:04:31

标签: python kivy kivy-language

我想将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:

1 个答案:

答案 0 :(得分:0)

尝试使用

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

并使用root.graph1

中的<KivyTesting>:对象