Python 2.7 Python:获取动态TextInput的id
我正在使用Python 2.7 Python:获取动态TextInput的id,python-2.7,kivy,Python 2.7,Kivy,我正在使用python-2.7和kivy。我运行test.py然后屏幕看起来像附加的图像。当我单击ok按钮时,我试图使用此代码获取TextInput的id。 for row in reversed(rows.children): for ch in reversed(row.children): if isinstance(ch, TextInput): print(ch.id) if ch.text == "" and c
python-2.7
和kivy
。我运行test.py
然后屏幕看起来像附加的图像。当我单击ok
按钮时,我试图使用此代码获取TextInput的id
。
for row in reversed(rows.children):
for ch in reversed(row.children):
if isinstance(ch, TextInput):
print(ch.id)
if ch.text == "" and ch.id=='test1':
print("TextInput is required")
ch.focus = True
break;
但是print(ch.id)
显示None
。有人能告诉我如何获取文本输入的id
如果我可以获得id
,那么我可以忽略value2
的空白值。
test.py
试验电压(千伏)
:
test1:test1
test2:test2
方向:“水平”
间距:0,5
按钮:
文本:root.button\u文本
大小\u提示\u x:.2
文本输入:
id:test1
大小\u提示\u x:.4
文本输入:
id:test2
大小\u提示\u x:.4
显示:
盒子布局:
方向:“垂直”
填充:20,20
盒子布局:
方向:“水平”
按钮:
大小\u提示\u x:.2
正文:“+添加更多”
valign:“底部”
按:root.add\u more()
盒子布局:
方向:“水平”
标签:
大小\u提示\u x:.2
正文:“SN”
valign:“底部”
标签:
大小\u提示\u x:.4
正文:“价值1”
valign:“底部”
标签:
大小\u提示\u x:.4
正文:“价值2”
valign:“底部”
排:
id:行
盒子布局:
方向:“水平”
填充:10,0
间距:10,10
大小提示:.5、.7
位置提示:{'x':.25,'y':.25}
按钮:
文字:“Ok”
发布时:
root.insert()
按钮:
文本:“取消”
发布时:root.disclose()
来自:
不过,您可以自己指定跑步id
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
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
Window.size = (450, 525)
class display(Screen):
def add_more(self):
self.ids.rows.add_row()
def insert(self):
values = []
rows = self.ids.rows
for row in reversed(rows.children):
for ch in reversed(row.children):
if isinstance(ch, TextInput):
print(ch.id)
if ch.text == "" and ch.id=='test1':
print("TextInput is required")
ch.focus = True
break;
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):
return self.root
test().run()
<Row>:
test1 : test1
test2 : test2
orientation: "horizontal"
spacing: 0, 5
Button:
text: root.button_text
size_hint_x: .2
TextInput:
id : test1
size_hint_x: .4
TextInput:
id : test2
size_hint_x: .4
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"
Label:
size_hint_x: .2
text: "SN"
valign: 'bottom'
Label:
size_hint_x: .4
text: "Value1"
valign: 'bottom'
Label:
size_hint_x: .4
text: "Value2"
valign: 'bottom'
Rows:
id: rows
BoxLayout:
orientation: "horizontal"
padding : 10, 0
spacing: 10, 10
size_hint: .5, .7
pos_hint: {'x': .25, 'y':.25}
Button:
text: 'Ok'
on_release:
root.insert()
Button:
text: 'Cancel'
on_release: root.dismiss()
id Added in 1.0.0
Unique identifier of the widget in the tree.
id is a StringProperty and defaults to None.