如何用Kivy获取textinput的值

2024-05-19 10:08:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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”填充字段文本的按钮必须允许我获取两个字段的值并将它们添加到列表中,但是:

^{pr2}$

在我的课上它是这样定义的:

class ProduitScreen(Screen):
    def ajouter():
        print "%s au prix de %d a ete ajoute" % (self.nom.txt , float(self.prix.txt))

有人能告诉我怎么做吗?在

编辑:黑引号不保存缩进,所以有完整的代码http://pastebin.com/W1WJ8NcL


Tags: textselfidsizeonbuttontextinputnom
1条回答
网友
1楼 · 发布于 2024-05-19 10:08:09

ajouter方法是ProduitScreen而不是{}的成员,因此应该使用root来引用它:

    Button:
        text: 'Ajouter'
        on_press: root.ajouter()

同时修复ajouter定义中的问题:

^{pr2}$

为了在python代码中使用nomprix,请将以下内容添加到kv代码中:

<ProduitScreen>:
    nom: nom
    prix: prix

相关问题 更多 >

    热门问题