如何使用 Kivy 获取文本输入的值

2023-06-06Python开发问题
19

本文介绍了如何使用 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 代码中使用 nomprix,请将其添加到 kv 代码中:

In order to use nom and prix inside your python code, add this to kv code:

<ProduitScreen>:
    nom: nom
    prix: prix

这篇关于如何使用 Kivy 获取文本输入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10