使用RecycleView时设置列的宽度

时间:2017-11-08 10:07:57

标签: python python-2.7 kivy kivy-language

user.py

import kivy

kivy.require('1.9.0')  # replace with your current kivy version !
import sqlite3 as lite
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty,NumericProperty
from kivy.lang import Builder

from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
from kivy.core.window import Window



Window.size = (500, 500)

#con = lite.connect('test.db')
#con.text_factory = str
#cur = con.cursor()


class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                  RecycleGridLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' Add selection support to the Button '''
    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(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableButton, 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





class RV(BoxLayout):
    data_items = ListProperty([])

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.get_users()

    def get_users(self):
        #cur.execute("SELECT * FROM `users` order by id asc")
        #rows = cur.fetchall()
        '''This result retrieve from database'''
        rows = [(1, 'Yash', 'Chopra'),(2, 'amit', 'Kumar')]

        for row in rows:
            for col in row:
                self.data_items.append(col)


class ListUser(App):
    title = "Users"

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



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

user.kv

    #:kivy 1.10.0

<SelectableButton>:
    # 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>:
    BoxLayout:
        orientation: "vertical"

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 3

            Label:
                text: "ID"
            Label:
                text: "First Name"
            Label:
                text: "Last Name"

        BoxLayout:
            RecycleView:
                viewclass: 'SelectableButton'
                data: [{'text': str(x)} for x in root.data_items]
                SelectableRecycleGridLayout:
                    cols: 3
                    default_size: None, dp(26)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'
                    multiselect: True
                    touch_multiselect: True

有人可以帮助我吗?  1.如何根据显示打开全尺寸窗口。还应显示标题栏,最小化,交叉选项  2.如何设置宽度ID = 20%和名字:40%和姓氏:40%。所有列都相同。

1 个答案:

答案 0 :(得分:4)

  
      
  1. 如何根据显示打开全尺寸窗口。还应显示标题栏,最小化,交叉选项。
  2.   

窗口小部件的最大化方法允许您以fullsize打开窗口并显示标题栏

  
      
  1. 如何设置宽度ID = 20%和名字:40%和姓氏:40%。所有列都相同。
  2.   

我认为你的第二个和第三个问题是相同的,你可以用3个回收视图代替一个,每列一个

<强>的.py

...

from kivy.core.window import Window
Window.maximize()

...

class RV(BoxLayout):
    col1 = ListProperty()
    col2 = ListProperty()
    col3 = ListProperty()

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.get_users()

    def get_users(self):
        #cur.execute("SELECT * FROM `users` order by id asc")
        #rows = cur.fetchall()
        '''This result retrieve from database'''
        rows = [(1, 'Yash', 'Chopra'),(2, 'amit', 'Kumar')]

        for row in rows:
            self.col1.append(row[0])
            self.col2.append(row[1])
            self.col3.append(row[2])

...

<强> .kv

...

<MyRV@RecycleView>:
    viewclass: 'SelectableButton'
    SelectableRecycleGridLayout:
        cols: 1
        default_size: None, dp(26)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: True
        touch_multiselect: True

<RV>:
    BoxLayout:
        orientation: "vertical"

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 3

            Label:
                size_hint_x: .2
                text: "ID"
            Label:
                size_hint_x: .4
                text: "First Name"
            Label:
                size_hint_x: .4
                text: "Last Name"

        BoxLayout:
            MyRV:
                size_hint_x: .2
                data: [{'text': str(x)} for x in root.col1]
            MyRV:
                size_hint_x: .4
                data: [{'text': str(x)} for x in root.col2]
            MyRV:
                size_hint_x: .4
                data: [{'text': str(x)} for x in root.col3]