如何用Kivy获取textinput的值

时间:2022-03-25 23:17:38

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.

我是Kivy的新手,因为我无法在PySide上练习(一些动态库坏了或者我不知道是什么)我想尝试这个巨大的工具。

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

我现在迷路了,我试着这样做:在Kivy app中获取textinput值

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 :

因此,带有'Ajouter'字段文本的Button必须允许我获取两个字段的值并将它们添加到列表中,但是:

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

编辑:blackquote不保存缩进,所以有完整的代码http://pastebin.com/W1WJ8NcL

1 个解决方案

#1


2  

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

ajouter方法是ProduitScreen的成员而不是Button,所以你应该使用root来引用它:

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

Also fix issues on your definition of ajouter:

还修复了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:

为了在你的python代码中使用nom和prix,将其添加到kv代码:

<ProduitScreen>:
    nom: nom
    prix: prix

#1


2  

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

ajouter方法是ProduitScreen的成员而不是Button,所以你应该使用root来引用它:

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

Also fix issues on your definition of ajouter:

还修复了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:

为了在你的python代码中使用nom和prix,将其添加到kv代码:

<ProduitScreen>:
    nom: nom
    prix: prix