Linking a Kivy Button to Function
<p>There is something in kivy I do not understand, and am hoping that someone could shed light. I have done a lot of reading in this topic but it just doesn't seem to be connecting in my head.</p>

<p>My issue comes from linking a function to a kivy button.
Right now I am trying to learn how to do a simple function:</p>

<pre><code>def Math():
    print 1+1
</code></pre>

<p>What I would like to do something more complex:</p>

<pre><code>def Math(a,b):
    print a^2 + b^2
</code></pre>

<p>Where <code>a</code> and <code>b</code> are input labels from kivy, and upon clicking a button the answer will be printed.</p>

<p>This is what I have so far:</p>

<pre><code>from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout


#######``Logics``#######
class Math(FloatLayout):
    def add(self):
        print 1+1

#######``Windows``#######
class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
   pass

class ScreenManagement(ScreenManager):
   pass


presentation = Builder.load_file("GUI_Style.kv")

class MainApp(App):
    def build(self):
       return presentation

if __name__ == "__main__":
    MainApp().run()
</code></pre>

<p>This is my kivy language file:</p>

<pre><code>import NoTransition kivy.uix.screenmanager.NoTransition

ScreenManagement:
    transition: NoTransition()
    MainScreen:
    AnotherScreen:

&lt;MainScreen&gt;:
    name: "main"
    FloatLayout:
        Button:
            on_release: app.root.current = "other"
            text: "Next Screen"
            font_size: 50
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3,0.2
            pos_hint: {"right":1, "top":1}

&lt;AnotherScreen&gt;:
    name: "other"
    FloatLayout:
        Button:
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3,0.2
            text: "add"
            pos_hint: {"x":0, "y":0}
            on_release: root.add
        Button:
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3,0.2
            text: "Back Home"
            on_release: app.root.current = "main"
            pos_hint: {"right":1, "top":1}
</code></pre>

<pre><code>&lt;AnotherScreen&gt;:
    name: "other"
    FloatLayout: 
        Button:
            ...
            on_release: root.add &lt;-- here *root* evaluates to the top widget in the rule.
</code></pre>

<p>Which is an AnotherScreen instance, but it doesn't have an <code>add</code> method.</p>

<pre><code>class Math(FloatLayout):
    def add(self):
        print 1+1
</code></pre>

<p>Here you have declared a Math class by inheriting from the <code>FloatLayout</code>, which is a uix component -a widget-. And you defined a method on this class <code>add</code>. Still you haven't used it. In the kv file you used <code>FloatLayout</code>.</p>

<p>Now in order for you to access a function in kv, most of the time you'll acces it as a method of a uix component, by using either <code>root</code>/<code>self</code> or <code>app</code>, you can also import it e.g: </p>

<pre><code>#: import get_color_from_hex kivy.utils.get_color_from_hex
&lt;ColoredWidget&gt;:
    canvas:
        Color: 
            rgba: get_color_from_hex("DCDCDC")
        Rectangle:
            size: self.size
            pos: self.pos 
</code></pre>

<p>So you can't do it like this:</p>

<pre><code>&lt;AnotherScreen&gt;:
    name: "other"
    Math:
        id: math_layout
        Button:
            ...
            on_release: math_layout.add()
</code></pre>

<p>or like this:</p>

<pre><code>class AnotherScreen(Screen):
   def add(self):
       print(1+1)
</code></pre>

<p>If you still have problems conserning this topic, i'll be glad to provide more help .</p>

python kivy kivy-language