Kivy Listview Excel文件 [英] Kivy Listview excel file

查看:115
本文介绍了Kivy Listview Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Kivy listview和Pandas数据框有疑问.具体来说,如何从.xlsx到kivy的listview中列出数据,然后说删除选定的条目.这是我的主要代码:

I have a question regarding Kivy listview and Pandas dataframes. Specifically how to list data from .xlsx to listview of kivy and then lets say delete selected entry.This is my main code:

import pandas
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.listview import ListItemButton

class ItemsListItemButton(ListItemButton):
    pass

class QuestionDb(BoxLayout):
    items_list = ObjectProperty()

    def dataframe(self):
        df = pandas.read_excel("items.xlsx")
        return df


class QuestionApp(App):
    def build(self):
        return QuestionDb()

Questionapp= QuestionApp()
Questionapp.run()

这是用来获取列表视图和按钮的question.kv文件

This is question.kv file used to get the listview and button

#: import main question
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import LitsItemButton kivy.uix.listview.ListItemButton


QuestionDb:

<QuestionDb>:
    items_list:  items_list_view
    ListView:
       id:items_list_view
       adapter:
           ListAdapter(data=main.QuestionDb.dataframe(self) , 
cls=main.ItemsListItemButton)
    Button:
        text: "Deletes selected entry on press"

这是我们设置为数据框的excel电子表格"items.xlsx":

And this is the excel spreadsheet "items.xlsx" which we set as our dataframe:

 Item:  Cost:   Remaining:
 Boots  10$     5
 Socks  2$      4
 Hats   5$      10

现在,此设置列表视图以kivy形式仅显示列名称,并且不列出其他任何项目,我该如何使它列出,例如:

Now with this setup listview in kivy only shows the column names and lists no other items, how can i make it so that items are listed example:

Boots  10$     5
Socks  2$     4
Hats   5$     10

代替这个

关于如何将按钮链接到之后删除所选条目的任何提示,也将不胜感激.

Also any tips on how to link the button to afterwards delete the selected entry would be appreciated as well.

希望这是有道理的.

推荐答案

您应使用Recycleview,因为

You should use Recycleview because Listview has been deprecated since version 1.10.0.

在下面的示例中,我们使用了 Recycleview 可选择的按钮回收网格布局. Recycleview支持向上和向下滚动.我们已经将按钮与 on_release 事件绑定了.您还可以更改按钮以绑定 on_press 事件.单击任何行将调用方法 delete_row .

In the example below, we are using a Recycleview with selectable recycle grid layout of buttons. Recycleview supports scrolling up and down. We have binded the button with on_release event. You can also change the button to bind on_press event. Click on any row will invoke the method, delete_row.

Kivy RecycleView文档

import pandas
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

from kivy.uix.label import Label
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.properties import BooleanProperty, ListProperty, ObjectProperty
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.core.window import Window


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


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' 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(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 QuestionDb(BoxLayout):
    items_list = ObjectProperty(None)
    column_headings = ObjectProperty(None)
    rv_data = ListProperty([])

    def __init__(self, **kwargs):
        super(QuestionDb, self).__init__(**kwargs)
        self.get_dataframe()

    def get_dataframe(self):
        df = pandas.read_excel("items.xlsx")

        # Extract and create column headings
        for heading in df.columns:
            self.column_headings.add_widget(Label(text=heading))

        # Extract and create rows
        data = []
        for row in df.itertuples():
            for i in range(1, len(row)):
                data.append([row[i], row[0]])
        self.rv_data = [{'text': str(x[0]), 'Index': str(x[1]), 'selectable': True} for x in data]

    def delete_row(self, instance):
        # TODO
        print("delete_row:")
        print("Button: text={0}, index={1}".format(instance.text, instance.index))
        print(self.rv_data[instance.index])
        print("Pandas: Index={}".format(self.rv_data[instance.index]['Index']))


class QuestionApp(App):
    def build(self):
        Window.clearcolor = (1, 1, 1, 1)    # white background
        return QuestionDb()


if __name__ == "__main__":
    QuestionApp().run()

question.kv

#:kivy 1.10.0

<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (0, 0.517, 0.705, 1) if self.selected else (0, 0.517, 0.705, 1)
        Rectangle:
            pos: self.pos
            size: self.size
    background_color: [1, 0, 0, 1]  if self.selected else [1, 1, 1, 1]  # dark red else dark grey
    on_release: app.root.delete_row(self)

<QuestionDb>:
    column_headings: column_headings
    orientation: "vertical"

    Label:
        canvas.before:
            Color:
                rgba: (0, 0, 1, .5)     # 50% translucent blue
            Rectangle:
                pos: self.pos
                size: self.size
        text: 'Click on any row to delete'
        size_hint: 1, 0.1

    GridLayout:
        canvas.before:
            Color:
                rgba: (1, 0.2, 0, .5)     # 50% translucent orange red
            Rectangle:
                pos: self.pos
                size: self.size

        id: column_headings
        size_hint: 1, None
        size_hint_y: None
        height: 25
        cols: 3

    BoxLayout:
        canvas.before:
            Color:
                rgba: (.0, 0.9, .1, .3)
            Rectangle:
                pos: self.pos
                size: self.size

        RecycleView:
            viewclass: 'SelectableButton'
            data: root.rv_data
            SelectableRecycleGridLayout:
                cols: 3
                key_selection: 'selectable'
                default_size: None, dp(26)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                multiselect: True
                touch_multiselect: True

输出

这篇关于Kivy Listview Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆