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