python : Count dynamic row

标签 python kivy

我正在使用 python-2.7kivy 1.10.0 。我使用 +Add More 按钮添加动态行。
我在每个动态 TextInput 中使用 on_text 事件。有人可以帮助我如何计算dynamicon_text事件吗?
在此图中,您可以看到2行。当我输入任何TextInput时,然后调用on_text事件。此时如何计算总数量行?

enter image description here

测试.py

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty

Window.size = (450, 525)


class display(Screen):

    def add_more(self):
        self.ids.rows.add_row()


class Row(BoxLayout):
    button_text = StringProperty("")

    def count_row(self):
        print('count row')


class Rows(BoxLayout):
    orientation = "vertical"
    row_count = 0

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count)))


class test(App):

    def build(self):
        #self.root = Builder.load_string(KV)
        return self.root


test().run()

测试.kv

<Row>:
    orientation: "horizontal"
    spacing: 0, 5

    Button:
        text: root.button_text
        size_hint_x: .2

    TextInput:
        size_hint_x: .8
        on_text: root.count_row()


display:

    BoxLayout:
        orientation: "vertical"
        padding : 20, 20

        BoxLayout:
            orientation: "horizontal"

            Button:
                size_hint_x: .2
                text: "+Add More"
                valign: 'bottom'
                on_press: root.add_more()


        BoxLayout:
            orientation: "horizontal"


        Rows:
            id: rows

最佳答案

如果您希望一个元素通知另一个元素,那么您必须使用属性(在您的情况下为 NumericProperty),并在它们之间创建绑定(bind)。

...
class Row(BoxLayout):
    button_text = StringProperty("")
    row_count = NumericProperty(0)
    def count_row(self):
        print('count row', self.row_count)

class Rows(BoxLayout):
    orientation = "vertical"
    row_count = NumericProperty(0)

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        self.row_count += 1
        row = Row(button_text=str(self.row_count))
        self.add_widget(row)
        row.row_count = self.row_count
        self.bind(row_count= row.setter("row_count"))
...

关于 python : Count dynamic row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50266390/

相关文章:

java - 将 Python POST 请求转换为 Android

python - `emoji` 和 `django-emoji` 包名冲突

android - Kivy 应用程序在设备上运行时不使用 VKeyboard

python - 通过smtplib发送邮件时如何在邮件内容中添加href链接

Python NLTK : parse sentence using conjoint structure, 进入无限递归

python - 如何使用在 kivy 中作为按钮按下时播放的动画 gif?

python - Kivy KV 文件自定义属性

python - 如何将标签的文本值设置为 .kv 文件中的全局变量?

python - key 错误 : 'kivy.garden.graph'

python - 使用 PIL 修剪空格