【问题标题】:Python : How to get value of dynmaic rowPython:如何获取动态行的值
【发布时间】:2018-01-04 07:47:49
【问题描述】:

我是 python 和 kivy 的新手。
我正在尝试获取动态行的价值。但现在我正在获得这样的价值

Column2
Column1

谁能告诉我如何获得这样的价值?

1,column1,column2
2,column1,column2

因为我的数据库表中有三列,例如 id、name、value,并且我想通过循环在数据库表中插入值
我正在使用此代码

def insert_value(self):
        values = []
        rows = self.ids.rows

        for row in reversed(rows.children):

            for ch in row.children:
                if isinstance(ch, TextInput):
                    values.append(ch.text)
        lenArray = len(values)

        for x in range(0, lenArray):
            print (values[x])

demo.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 BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.uix.textinput import TextInput

Window.size = (450, 525)


class display(Screen):

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

    def insert_value(self):
        values = []
        rows = self.ids.rows

        for row in reversed(rows.children):

            for ch in row.children:
                if isinstance(ch, TextInput):
                    values.append(ch.text)
        lenArray = len(values)

        for x in range(0, lenArray):
            print (values[x])



class Row(BoxLayout):
    button_text = StringProperty("")
    id = ObjectProperty(None)



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),id=str("test"+str(self.row_count))))


class test(App):

    def build(self):
        self.root = Builder.load_file('demo.kv')
        return self.root

if __name__ == '__main__':
    test().run()

demo.kv

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

    Button:
        text: root.button_text
        size_hint_x: .2

    TextInput:
        text:"Column1"
        size_hint_x: .8

    TextInput:
        text:"Column2"
        size_hint_x: .8
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: .8
                text: "Value"
                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_value()

            Button:
                text: 'Cancel'
                on_release: root.dismiss()

【问题讨论】:

    标签: python python-3.x python-2.7 kivy kivy-language


    【解决方案1】:

    为了保持结构,我们必须创建一个列表列表,然后在每个列表中,第一个参数是我们通过isinstance() 过滤的 Button 的文本,其他元素是串联的。

    [...]
    from kivy.uix.button import Button
    
    class display(Screen):
    
        def add_more(self):
            self.ids.rows.add_row()
    
        def insert_value(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)
    
            for val in values:
                print("{},{},{}".format(*val))
    
    [...]
    

    【讨论】:

      【解决方案2】:

      一种选择是将ListProperty 添加到您的Row 类中,该类以您想要的顺序存储行的值,这样以后更容易获取它们。

      您可以使用ListView 来显示行。

      Demo.kv:

      <Row>:
          values: row_id.text, col1.text, col2.text
          orientation: "horizontal"
          spacing: 0, 5
          size_hint_y: None
          height: 30
      
          Button:
              id: row_id
              text: root.button_text
              size_hint_x: .2
      
          TextInput:
              id: col1
              text:"Column1"
              size_hint_x: .8
      
          TextInput:
              id: col2
              text:"Column2"
              size_hint_x: .8
      
      <Rows>:
          content: content
          BoxLayout:
              id: content
              orientation: "vertical"
              size_hint_y: None
              height: self.minimum_height
      
      Display:
          rows: rows
          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: .8
                      text: "Value"
                      valign: 'bottom'
      
      
              Rows:
                  id: rows
      
              BoxLayout:
                  orientation: "horizontal"
                  padding : 10, 10
                  spacing: 10, 10
                  size_hint: .5, .7
                  pos_hint: {'x': .25, 'y':.25}
      
                  Button:
                      text: 'Ok'
                      on_release:
                          root.insert_value()
      
                  Button:
                      text: 'Cancel'
                      on_release: root.dismiss()
      

      Demo.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 ListProperty, StringProperty, ObjectProperty
      from kivy.uix.scrollview import ScrollView
      from kivy.clock import Clock
      
      Window.size = (450, 525)
      
      
      class Display(Screen):
          rows = ObjectProperty(None)
      
          def __init__(self, **kwargs):
              super(Display, self).__init__(**kwargs)
      
          def add_more(self):
              self.rows.add_row()
      
          def insert_value(self):
              values = [row.values for row in reversed(self.rows.content.children)]
              for row in values:
                  print(row)
      
      
      class Row(BoxLayout):
          button_text = StringProperty("")
          id = ObjectProperty(None)
          values = ListProperty()
      
      
      class Rows(ScrollView):
          row_count = 0
          content = ObjectProperty(None)
      
          def __init__(self, **kwargs):
              super(Rows, self).__init__(**kwargs)
              Clock.schedule_once(self.add_row)
      
          def add_row(self, *args):
              self.row_count += 1
              self.content.add_widget(Row(button_text=str(self.row_count),
                                      id="test" + str(self.row_count)))
      
      
      class Test(App):
      
          def build(self):
              self.root = Builder.load_file('Demo.kv')
              return self.root
      
      if __name__ == '__main__':
          Test().run()
      

      【讨论】:

        猜你喜欢
        • 2012-01-04
        • 1970-01-01
        • 2021-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        相关资源
        最近更新 更多