簡體   English   中英

如何用Kivy獲取textinput的值

[英]How to get value of textinput with Kivy

我是Kivy的新手,因為我無法在PySide上練習(一些動態庫壞了或我不知道是什么)我想嘗試這個巨大的工具。

我現在迷路了,我試着這樣做: 在Kivy app中獲取textinput值

但我不是以同樣的方式做到這一點:

<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'字段文本的Button必須允許我獲取兩個字段的值並將它們添加到列表中,但是:

AttributeError: 'Button' object has no attribute 'ajouter'

在我的課堂上,它定義如下:

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代碼中使用nomprix ,將其添加到kv代碼:

<ProduitScreen>:
    nom: nom
    prix: prix

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM