如何使用 Kivy 获取 textinput 的值
How to get value of textinput with Kivy
我是 Kivy 的新手,因为我无法在 PySide 上练习(一些动态库坏了或者我不知道是什么)我想试试这个巨大的工具。
我现在迷路了,我试着这样做:
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' 的按钮必须允许我获取两个字段的值并将它们添加到列表中,但是:
AttributeError: 'Button' object has no attribute 'ajouter'
在我的 class 中,它是这样定义的:
class ProduitScreen(Screen):
def ajouter():
print "%s au prix de %d a ete ajoute" % (self.nom.txt , float(self.prix.txt))
有人可以告诉我该怎么做吗?
编辑:blackquote 不保存缩进,所以有完整的代码 http://pastebin.com/W1WJ8NcL
ajouter
方法是 ProduitScreen
而非 Button
的成员,因此您应该使用 root
来引用它:
Button:
text: 'Ajouter'
on_press: root.ajouter()
同时修复关于 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 代码中:
<ProduitScreen>:
nom: nom
prix: prix
我是 Kivy 的新手,因为我无法在 PySide 上练习(一些动态库坏了或者我不知道是什么)我想试试这个巨大的工具。
我现在迷路了,我试着这样做: 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' 的按钮必须允许我获取两个字段的值并将它们添加到列表中,但是:
AttributeError: 'Button' object has no attribute 'ajouter'
在我的 class 中,它是这样定义的:
class ProduitScreen(Screen):
def ajouter():
print "%s au prix de %d a ete ajoute" % (self.nom.txt , float(self.prix.txt))
有人可以告诉我该怎么做吗?
编辑:blackquote 不保存缩进,所以有完整的代码 http://pastebin.com/W1WJ8NcL
ajouter
方法是 ProduitScreen
而非 Button
的成员,因此您应该使用 root
来引用它:
Button:
text: 'Ajouter'
on_press: root.ajouter()
同时修复关于 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 代码中:
<ProduitScreen>:
nom: nom
prix: prix