2016-11-05 73 views
0

我想將圖形從NiceGraph類導入到.kv文件,但我不知道該怎麼做。如何從Kivy的其他課程導入圖表?

我參考了文檔,但在班級中找不到任何有關使用班級的內容。

這是我的嘗試。

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: 

回答

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 

<KivyTesting>:

root.graph1對象工作