Using Kivy Garden Graph in KV

How to use garden.graph kivy module inside kv file? I just found the documentation explaining how to use it in the main python script.

I imported kivy.garden.graph into a python file, and I can add Graph inside the kv file, but I did not find documentation on how to set size, graphs, etc.

 Graph: id: graph_test plot: MeshLinePlot 

this gives an error with MeshLinePlot is not defined , although I imported it on the python side.

any help would be greatly appreciated, maybe we could then add this information to the github readme graph.

+10
python graph kivy
source share
4 answers

Building on the answer from piwnk:

I added this to the .kv file:

 #:import MeshLinePlot kivy.garden.graph.MeshLinePlot <SetGraph>: graph_test : graph_test Graph: id: graph_test plot: MeshLinePlot xlabel:'X' ylabel:'Y' x_ticks_minor:5 x_tics_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 pos: 0, root.height / 6 size: root.width * 2 / 3 , root.height * 18 / 24 

In main.py, I added:

 from math import sin from kivy.garden.graph import Graph, MeshLinePlot class SetGraph(Widget): graph_test = ObjectProperty(None) update_graph(self): plot = MeshLinePlot(color=[1, 0, 0, 1]) plot.points = [(x, sin(x / 10.)) for x in range(0, 101)] self.graph_test.add_plot(plot) class graphLayoutApp(App): def build(self): disp = SetGraph() disp.update_graph() return disp if __name__ == '__main__': graphLayoutApp().run() 

I changed my original proven solution to more descriptive names. I hope I made no mistakes. Let me know if the solution is not complete.

+3
source share

There was the same problem. Here's the solution:

As a rule, according to the kivy documentation, in the kv file:

 #:import name xyz 

equivalent to:

 from xy import z as name 

So you should use the following:

 #:import MeshLinePlot kivy.garden.graph.MeshLinePlot 

Worked in my case with the Graph class, but honestly, I have not yet been able to add this graph to the graph.

+1
source share

I think everything was on the right track. Using #: import should be able to import the file.

write this in the kv file:

 #:import MeshLinePlot 

it should be able to import the module, as kv documentation shows, also

0
source share

The answer from Mattis Aspa was very helpful, but for me it didn’t quite work. I'm new to this, so maybe these things are too obvious to point out. But in case this helps someone else at my level, I should have:

  1. Indent the properties under the Graph: declaration in the kv file (to bypass the "invalid data after declaration" exception from the kv parser.

  2. Add this includes:

    language: lang-py

     from kivy.properties import ObjectProperty from kivy.app import App from kivy.uix.widget import Widget 

to the top of the Python file.

  1. Name the kv file according to the definition of the application class, like this: graphLayout.kv (I named it graph.kv, so it was simply ignored - newbie mistake!)

  2. I get an "invalid property name" for graph_test: graph_test. So I commented out this and used the identifier instead, changing the line

     self.graph_test.add_plot(plot) 

    so that:

     self.ids["graph_test"].add_plot(plot) 

I bet that at least some of these changes are related to version differences in kivy, so for clarity, I use kivy 1.9.1 and python 2.7.13.

0
source share

All Articles