Kivy Listview excel fi

2024-10-01 17:41:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于KivyListView和熊猫数据帧的问题。特别是如何将.xlsx中的数据列表到kivy的listview中,然后让我们假设delete selected入口。这个我的主要代码是:

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()

这是问题.kv用于获取listview和按钮的文件

^{pr2}$

这是excel电子表格”项目.xlsx“我们将其设置为数据帧:

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

现在kivy中的这个设置listview只显示列名而不列出其他项目,我如何才能使项目被列出例如:

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

而不是这个

此外,任何关于如何链接按钮,然后删除所选条目的提示,也将不胜感激。在

希望这有道理。在


Tags: 数据项目fromimportapppandasxlsxclass
1条回答
网友
1楼 · 发布于 2024-10-01 17:41:06

您应该使用Recycleview,因为Listview has been deprecated since version 1.10.0。在

在下面的例子中,我们使用的是一个循环查看按钮的可选择循环网格布局。Recycleview支持上下滚动。我们已将按钮与on_release事件绑定在一起。您也可以将按钮更改为在按下事件时绑定。单击任何行将调用方法,delete\u row。在

Kivy RecycleView Documentation

示例

问题.py在

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()

问题.kv在

^{pr2}$

输出

Img01 - Clicked Row 2

相关问题 更多 >

    热门问题