問題描述
我是 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
這篇關于如何使用 Kivy 獲取文本輸入的值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!