如何使用Kivy获取文本输入的价值 [英] How to get value of textinput with Kivy
问题描述
我是Kivy的新手,由于我无法在PySide上进行练习(某些动态库损坏或我不知道是什么),所以我想尝试使用此大型工具.
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应用中获取textinput值
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'
所以,字段文本用'Ajouter'填充的Button必须允许我获取两个字段的值并将它们添加到列表中,但是:
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 ?
黑引号不会保存缩进,因此有完整的代码 http://pastebin.com/W1WJ8NcL
EDIT : The blackquote doesn't save the indentation so there is the full code http://pastebin.com/W1WJ8NcL
推荐答案
ajouter
方法是ProduitScreen
而不是Button
的成员,因此您应该使用root
来引用它:
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()
还解决了有关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))
为了在Python代码中使用nom
和prix
,请将其添加到kv代码中:
In order to use nom
and prix
inside your python code, add this to kv code:
<ProduitScreen>:
nom: nom
prix: prix
这篇关于如何使用Kivy获取文本输入的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!