Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Kivy Garden Graph in KV language

Tags:

python

graph

kivy

How do I use the kivy module garden.graph inside the kv file? I only found documentation that explained how to use it in the main python script.

I imported the kivy.garden.graph in the python file, and I can add the Graph inside the kv file, but I didn't find any documentation how to set the size, plots etc.

Graph:
    id: graph_test
    plot: MeshLinePlot

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

any help would be highly appreciated, maybe we could then add this info to the graph's github readme as well.

like image 274
user3603948 Avatar asked Jun 13 '15 16:06

user3603948


People also ask

What is KV in Kivy?

kv. If this file defines a Root Widget it will be attached to the App's root attribute and used as the base of the application widget tree. Builder : You can tell Kivy to directly load a string or a file. If this string or file defines a root widget, it will be returned by the method: Builder.

What is Kivy garden?

Garden is a project to centralize addons for Kivy maintained by users. You can find more information at Kivy Garden. All the garden packages are centralized on the kivy-garden Github repository. The garden flower widgets are contributed by regular users such as yourself.

What is Kivy Lang?

The Kivy language is a language dedicated to describing user interface and interactions. You could compare this language to Qt's QML (http://qt.nokia.com), but we included new concepts such as rule definitions (which are somewhat akin to what you may know from CSS), templating and so on.


2 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 have changed my original tested solution to more descriptive names. Hopefully, I have not made any mistakes. Let me know if the solution is not complete.

like image 149
Mattis Asp Avatar answered Sep 20 '22 18:09

Mattis Asp


The answer from Mattis Asp was very helpful but didn't quite work for me. I am new to this, so maybe these things are too obvious to need stating. But in case it helps someone else at my level, I had to:

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

  2. Add these 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 to match the app class definition, so: graphLayout.kv (I had called it graph.kv so it was just ignored -- newbie mistake!)

  2. I was getting "invalid property name" for graph_test : graph_test. So I commented that out and used the id instead, changing the line

    self.graph_test.add_plot(plot)  
    

    to:

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

I bet at least some of these changes have to do with version differences in kivy so, for clarity, I am using kivy 1.9.1 and python 2.7.13.

like image 22
Ben Dugan Avatar answered Sep 21 '22 18:09

Ben Dugan