问题描述
我是 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设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)