Jérémy JOKE
Jérémy JOKE

Reputation: 281

How to get value of textinput with Kivy

I'm new to Kivy and as i'm not able to practice on PySide (some dynamic libraries broken or i don't know what) i want to try this huge tool.

I'm lost right now, i tried to do like this : Get textinput value in Kivy app

But i don't do this in the same way :

<ProduitScreen>:
    GridLayout:
        rows: 3
        cols: 2
        padding: 10
        spacing: 10
        Label:
            font_size: 20
            text: 'Nom du produit'
        TextInput:
            font_size: 20
            id: nom
        Label:
            font_size: 20
            text: 'Prix'
        TextInput:
            font_size: 20
            id: prix
        Button:
            text: 'Ajouter'
            on_press: self.ajouter()
        Button:
            text: 'Quitter'
            on_press: root.manager.current = 'menu'

So, the Button with the field text filled with 'Ajouter' has to permit me to get the value of both fields and add them into a list but :

AttributeError: 'Button' object has no attribute 'ajouter'

And in my class it's defined like that :

class ProduitScreen(Screen):
    def ajouter():
        print "%s au prix de %d a ete ajoute" % (self.nom.txt , float(self.prix.txt))

Does someone can tell me how to do that ?

EDIT : The blackquote doesn't save the indentation so there is the full code http://pastebin.com/W1WJ8NcL

Upvotes: 4

Views: 3667

Answers (1)

Assem
Assem

Reputation: 12077

ajouter method is a member of ProduitScreen not Button so you should use root to refer to it:

    Button:
        text: 'Ajouter'
        on_press: root.ajouter()

Also fix issues on your definition of ajouter:

class ProduitScreen(Screen):
    def ajouter(self):
        print "%s au prix de %f a ete ajoute" % (self.nom.text , float(self.prix.text))

In order to use nom and prix inside your python code, add this to kv code:

<ProduitScreen>:
    nom: nom
    prix: prix

Upvotes: 3

Related Questions