<small id='4kTX7'></small><noframes id='4kTX7'>

      <i id='4kTX7'><tr id='4kTX7'><dt id='4kTX7'><q id='4kTX7'><span id='4kTX7'><b id='4kTX7'><form id='4kTX7'><ins id='4kTX7'></ins><ul id='4kTX7'></ul><sub id='4kTX7'></sub></form><legend id='4kTX7'></legend><bdo id='4kTX7'><pre id='4kTX7'><center id='4kTX7'></center></pre></bdo></b><th id='4kTX7'></th></span></q></dt></tr></i><div id='4kTX7'><tfoot id='4kTX7'></tfoot><dl id='4kTX7'><fieldset id='4kTX7'></fieldset></dl></div>

        • <bdo id='4kTX7'></bdo><ul id='4kTX7'></ul>
        <legend id='4kTX7'><style id='4kTX7'><dir id='4kTX7'><q id='4kTX7'></q></dir></style></legend>
        <tfoot id='4kTX7'></tfoot>
      1. Kivy - python - recycleview 行中的多个小部件

        Kivy - python - multiple widgets in recycleview row(Kivy - python - recycleview 行中的多个小部件)

        <legend id='2rDF7'><style id='2rDF7'><dir id='2rDF7'><q id='2rDF7'></q></dir></style></legend>

        <i id='2rDF7'><tr id='2rDF7'><dt id='2rDF7'><q id='2rDF7'><span id='2rDF7'><b id='2rDF7'><form id='2rDF7'><ins id='2rDF7'></ins><ul id='2rDF7'></ul><sub id='2rDF7'></sub></form><legend id='2rDF7'></legend><bdo id='2rDF7'><pre id='2rDF7'><center id='2rDF7'></center></pre></bdo></b><th id='2rDF7'></th></span></q></dt></tr></i><div id='2rDF7'><tfoot id='2rDF7'></tfoot><dl id='2rDF7'><fieldset id='2rDF7'></fieldset></dl></div>
        • <bdo id='2rDF7'></bdo><ul id='2rDF7'></ul>

          • <tfoot id='2rDF7'></tfoot>

            <small id='2rDF7'></small><noframes id='2rDF7'>

              <tbody id='2rDF7'></tbody>

                • 本文介绍了Kivy - python - recycleview 行中的多个小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I would like to make a recycleview that has multiple labels in each recycleview row. In my specfic example I would like to have 3 labels in each row: 1 label containing the item index, one label containing an item from one dataset, and another label from another dataset

                  In this example (taken from the kivy examples) we have a recycleview where each row in the recycleview contains a single label:

                  from kivy.app import App
                  from kivy.lang import Builder
                  from kivy.uix.recycleview import RecycleView
                  from kivy.uix.recycleview.views import RecycleDataViewBehavior
                  from kivy.uix.label import Label
                  from kivy.properties import BooleanProperty
                  from kivy.uix.recycleboxlayout import RecycleBoxLayout
                  from kivy.uix.behaviors import FocusBehavior
                  from kivy.uix.recycleview.layout import LayoutSelectionBehavior
                  
                  Builder.load_string('''
                  <SelectableLabel>:
                      # Draw a background to indicate selection
                      canvas.before:
                          Color:
                              rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
                          Rectangle:
                              pos: self.pos
                              size: self.size
                  <RV>:
                      viewclass: 'SelectableLabel'
                      SelectableRecycleBoxLayout:
                          default_size: None, dp(56)
                          default_size_hint: 1, None
                          size_hint_y: None
                          height: self.minimum_height
                          orientation: 'vertical'
                          multiselect: True
                          touch_multiselect: True
                  ''')
                  
                  
                  items_1= {'apple', 'banana', 'pear', 'pineapple'}
                  items_2= {'dog', 'cat', 'rat', 'bat'}
                  
                  class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                                   RecycleBoxLayout):
                      ''' Adds selection and focus behaviour to the view. '''
                  
                  
                  class SelectableLabel(RecycleDataViewBehavior, Label):
                      ''' Add selection support to the Label '''
                      index = None
                      selected = BooleanProperty(False)
                      selectable = BooleanProperty(True)
                  
                      def refresh_view_attrs(self, rv, index, data):
                          ''' Catch and handle the view changes '''
                          self.index = index
                          return super(SelectableLabel, self).refresh_view_attrs(
                              rv, index, data)
                  
                      def on_touch_down(self, touch):
                          ''' Add selection on touch down '''
                          if super(SelectableLabel, self).on_touch_down(touch):
                              return True
                          if self.collide_point(*touch.pos) and self.selectable:
                              return self.parent.select_with_touch(self.index, touch)
                  
                      def apply_selection(self, rv, index, is_selected):
                          ''' Respond to the selection of items in the view. '''
                          self.selected = is_selected
                          if is_selected:
                              print("selection changed to {0}".format(rv.data[index]))
                          else:
                              print("selection removed for {0}".format(rv.data[index]))
                  
                  
                  class RV(RecycleView):
                      def __init__(self, **kwargs):
                          super(RV, self).__init__(**kwargs)
                          self.data = [{'text': str(x)} for x in items_1]
                  
                  
                  class TestApp(App):
                      def build(self):
                          return RV()
                  
                  if __name__ == '__main__':
                      TestApp().run()
                  

                  I would like each recycleview row to have 3 labels: first label is the index, second label is items_1 and third label is items_2. Like this:

                  0 apple dog

                  1 banana cat

                  2 pear rat

                  3 pineapple bat

                  Thank you!

                  解决方案

                  I was looking for a similar solution and could not find it. I do not think PalimPalim has really answered the question since I think the Ben t was looking for multiple label widgets treated as a single line.

                  In this case you use a custom widget for GridLayout and specify it's structure.

                  <SelectableLabel>:
                  # Draw a background to indicate selection
                  canvas.before:
                      Color:
                          rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
                      Rectangle:
                          pos: self.pos
                          size: self.size
                  label1_text: 'label 1 text'
                  label2_text: 'label 2 text'
                  label3_text: 'label 3 text'
                  pos: self.pos
                  size: self.size
                  Label:
                      id: id_label1
                      text: root.label1_text
                  Label:
                      id: id_label2
                      text: root.label2_text
                  Label:
                      id: id_label3
                      text: root.label3_text
                  

                  In applying your data into the RV, you will need to restructure the dictionary to reflect the label layout

                  class RV(RecycleView):
                  def __init__(self, **kwargs):
                      super(RV, self).__init__(**kwargs)
                      paired_iter = zip(items_1, items_2)
                      self.data = []
                      for i1, i2 in paired_iter:
                          d = {'label2': {'text': i1}, 'label3': {'text': i2}}
                          self.data.append(d)
                  

                  Finally in the refresh_view_attrs, you will specify .label_text which is bound to each label, or you can use label id's.

                  def refresh_view_attrs(self, rv, index, data):
                      ''' Catch and handle the view changes '''
                      self.index = index
                      self.label1_text = str(index)
                      self.label2_text = data['label2']['text']
                      self.ids['id_label3'].text = data['label3']['text']  # As an alternate method of assignment
                      return super(SelectableLabel, self).refresh_view_attrs(
                          rv, index, data)
                  

                  The entire code is below:

                  from kivy.app import App
                  from kivy.lang import Builder
                  from kivy.uix.recycleview import RecycleView
                  from kivy.uix.recycleview.views import RecycleDataViewBehavior
                  from kivy.uix.label import Label
                  from kivy.uix.gridlayout import GridLayout
                  from kivy.properties import BooleanProperty
                  from kivy.uix.recycleboxlayout import RecycleBoxLayout
                  from kivy.uix.behaviors import FocusBehavior
                  from kivy.uix.recycleview.layout import LayoutSelectionBehavior
                  
                  Builder.load_string('''
                  <SelectableLabel>:
                      # Draw a background to indicate selection
                      canvas.before:
                          Color:
                              rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
                          Rectangle:
                              pos: self.pos
                              size: self.size
                      label1_text: 'label 1 text'
                      label2_text: 'label 2 text'
                      label3_text: 'label 3 text'
                      pos: self.pos
                      size: self.size
                      Label:
                          id: id_label1
                          text: root.label1_text
                      Label:
                          id: id_label2
                          text: root.label2_text
                      Label:
                          id: id_label3
                          text: root.label3_text
                  
                  <RV>:
                      viewclass: 'SelectableLabel'
                      SelectableRecycleBoxLayout:
                          default_size: None, dp(56)
                          default_size_hint: 1, None
                          size_hint_y: None
                          height: self.minimum_height
                          orientation: 'vertical'
                          multiselect: True
                          touch_multiselect: True
                  ''')
                  
                  
                  items_1 = {'apple', 'banana', 'pear', 'pineapple'}
                  items_2 = {'dog', 'cat', 'rat', 'bat'}
                  
                  
                  class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                                   RecycleBoxLayout):
                      ''' Adds selection and focus behaviour to the view. '''
                  
                  
                  class SelectableLabel(RecycleDataViewBehavior, GridLayout):
                      ''' Add selection support to the Label '''
                      index = None
                      selected = BooleanProperty(False)
                      selectable = BooleanProperty(True)
                      cols = 3
                  
                      def refresh_view_attrs(self, rv, index, data):
                          ''' Catch and handle the view changes '''
                          self.index = index
                          self.label1_text = str(index)
                          self.label2_text = data['label2']['text']
                          self.ids['id_label3'].text = data['label3']['text']  # As an alternate method of assignment
                          return super(SelectableLabel, self).refresh_view_attrs(
                              rv, index, data)
                  
                      def on_touch_down(self, touch):
                          ''' Add selection on touch down '''
                          if super(SelectableLabel, self).on_touch_down(touch):
                              return True
                          if self.collide_point(*touch.pos) and self.selectable:
                              return self.parent.select_with_touch(self.index, touch)
                  
                      def apply_selection(self, rv, index, is_selected):
                          ''' Respond to the selection of items in the view. '''
                          self.selected = is_selected
                          if is_selected:
                              print("selection changed to {0}".format(rv.data[index]))
                          else:
                              print("selection removed for {0}".format(rv.data[index]))
                  
                  
                  class RV(RecycleView):
                      def __init__(self, **kwargs):
                          super(RV, self).__init__(**kwargs)
                          paired_iter = zip(items_1, items_2)
                          self.data = []
                          for i1, i2 in paired_iter:
                              d = {'label2': {'text': i1}, 'label3': {'text': i2}}
                              self.data.append(d)
                          # can also be performed in a complicated one liner for those who like it tricky
                          # self.data = [{'label2': {'text': i1}, 'label3': {'text': i2}} for i1, i2 in zip(items_1, items_2)]
                  
                  
                  class TestApp(App):
                      def build(self):
                          return RV()
                  
                  if __name__ == '__main__':
                      TestApp().run()
                  

                  这篇关于Kivy - python - recycleview 行中的多个小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                    • <bdo id='gKdvx'></bdo><ul id='gKdvx'></ul>

                      <i id='gKdvx'><tr id='gKdvx'><dt id='gKdvx'><q id='gKdvx'><span id='gKdvx'><b id='gKdvx'><form id='gKdvx'><ins id='gKdvx'></ins><ul id='gKdvx'></ul><sub id='gKdvx'></sub></form><legend id='gKdvx'></legend><bdo id='gKdvx'><pre id='gKdvx'><center id='gKdvx'></center></pre></bdo></b><th id='gKdvx'></th></span></q></dt></tr></i><div id='gKdvx'><tfoot id='gKdvx'></tfoot><dl id='gKdvx'><fieldset id='gKdvx'></fieldset></dl></div>

                    • <small id='gKdvx'></small><noframes id='gKdvx'>

                      <legend id='gKdvx'><style id='gKdvx'><dir id='gKdvx'><q id='gKdvx'></q></dir></style></legend>
                    • <tfoot id='gKdvx'></tfoot>

                          <tbody id='gKdvx'></tbody>