代码之家  ›  专栏  ›  技术社区  ›  Nirdesh Kumawat

Python:如何设置对动态文本输入的关注

  •  0
  • Nirdesh Kumawat  · 技术社区  · 6 年前

    我正在使用 python-2.7 kivy
    当我跑步时 test.py 然后屏幕看起来像附加的图像。当我点击 +Add More 按钮,然后将添加行 dynamic 。如果我在单击后像附加图像一样填充值 Ok 按钮,然后我可以找到空白值 TextInput 使用此代码。

            a = 1
            for val in values:
                print(val)
                if val[1] == "":
                    print("row"+str(a) +" TextInput 1 is required")
                    break;
    
                if val[2] == "":
                    print("row"+str(a) +" TextInput 2 is required")
                    break;
                a = a+1
    

    但有人能告诉我如何将焦点设置为第一个空格吗 文本输入框 喜欢附加的图像。

    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
    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):
                vals = []
                for ch in reversed(row.children):
                    if isinstance(ch, TextInput):
                        vals.append(ch.text)
                    if isinstance(ch, Button):
                        vals.insert(0, ch.text)
                values.append(vals)
    
            a = 1
            for val in values:
                print(val)
                if val[1] == "":
                    print("row"+str(a) +" TextInput 1 is required")
                    break;
    
                if val[2] == "":
                    print("row"+str(a) +" TextInput 2 is required")
                    break;
                a = a+1
    
    
    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>:
        orientation: "horizontal"
        spacing: 0, 5
    
        Button:
            text: root.button_text
            size_hint_x: .2
    
        TextInput:
            size_hint_x: .4
    
        TextInput:
            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()
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   ikolim    6 年前

    忽略空白值每行的第一列(值1)

    1. 在kv文件中,删除子项 文本输入框 类中的小部件 一行
    2. 在Python脚本中,当实例化第一个 文本输入框 widget,提供 id='value1'

    有关详细信息,请参阅下面的代码片段。

    代码段

    主要的py公司

    class display(Screen):
    
        def add_more(self):
            self.ids.rows.add_row()
    
        def insert(self):
    
            rows = self.ids.rows
    
            for row in reversed(rows.children):
                for ch in reversed(row.children):
                    if isinstance(ch, TextInput):
                        if ch.text == "" and ch.id != 'value1':
                            print("\nch.id=", ch.id)
                            print("TextInput is required")
                            ch.focus = True
                            break;
    
    
    class Row(BoxLayout):
        button_text = StringProperty("")
    
        def __init__(self, **kwargs):
            super(Row, self).__init__(**kwargs)
            self.add_widget(TextInput(id="value1", size_hint_x=0.4))
            self.add_widget(TextInput(size_hint_x=0.4))
    

    测验千伏

    <Row>:
        orientation: "horizontal"
        spacing: 0, 5
    
        Button:
            text: root.button_text
            size_hint_x: .2
    

    检查每行的文本输入是否为空值

    解决方案如下:

    代码段

    class display(Screen):
    
        def add_more(self):
            self.ids.rows.add_row()
    
        def insert(self):
    
            rows = self.ids.rows
    
            for row in reversed(rows.children):
                for ch in reversed(row.children):
                    if isinstance(ch, TextInput):
                        if ch.text == "":
                            print("TextInput is required")
                            ch.focus = True
                            break;
    

    输出-忽略空白值每行的第一列(值1)

    Img01 Img02

    输出-检查每行的文本输入是否为空值

    Img01 Img02