如何使用 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 应用中获取文本输入值
I'm lost right now, i tried to do like this : Get textinput value in Kivy app
但我不这样做:
<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"字段文本的按钮必须允许我获取两个字段的值并将它们添加到列表中,但是:
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 ?
blackquote 不保存缩进,所以有完整的代码 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
相关文章