How to get value of textinput with Kivy(如何使用 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
这篇关于如何使用 Kivy 获取文本输入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Kivy 获取文本输入的值


基础教程推荐
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01