【问题标题】:Kivy: making a table widgetKivy:制作表格小部件
【发布时间】:2017-09-18 14:33:59
【问题描述】:

我正在使用 GridLayout 和 ScrollView 在表格小部件中使用 kivy。这就是我所拥有的:

https://github.com/Skucul/listwidget

有更好的方法来实现它吗? RecicleView 呢?

【问题讨论】:

  • RecycleView 如果你需要它会更强大。
  • @EL3PHANTEN,我从未使用过回收视图。如何使用 RecycleView 创建列和行?你能发布一个简单的例子吗?

标签: python-3.x android-recyclerview widget kivy


【解决方案1】:

我使用 RecycleView 和 KivyMD 获得了非常好的结果,它具有带有图标和图像等的列表元素。

我也看到了您的其他存储库,例如 Datepicker 和 Timepicker,有类似的,在 KivyMD 中有非常相似的小部件,也许您会考虑使用它们甚至更好,增强它们。

【讨论】:

  • KivyMD 看起来很棒!您是否尝试使用 pyinstaller 冻结您的应用程序?您的 KivyMD 应用程序是桌面应用程序吗?非常感谢!
【解决方案2】:
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivymd.label import MDLabel
from kivy.uix.button import Button
from kivy.properties import NumericProperty, ListProperty,StringProperty
from kivy.graphics import Color
from kivy.metrics import dp

Builder.load_string('''
#:import MDLabel kivymd.label.MDLabel
<Table>
    orientation:'vertical'  
    size_hint_y:0.9
    GridLayout:
        id:header
        spacing:2
        padding:[10,10,10,10]
        size_hint_y:None
        height:dp(48)
    ScrollView:
        size_hint_y:1       
        GridLayout:
            id:body
            spacing:2
            padding:[10,10,10,10]
            size_hint_y:None
            #spacing:dp(2)
            height:self.minimum_height

    # GridLayout:
    #     id:footer
    #     height:dp(48)
    #     pos_hint:{'center_y':0.1}

<Header>
    padding:[10,10,10,10]
    canvas.before:
        Color:
            rgba: app.theme_cls.accent_dark
        Rectangle:
            pos: self.pos
            size: self.size
    size_hint_y:None
    size_hint_x:header.size_hint_x
    height:dp(48)
    MDLabel:
        id:header
        text:root.text
<Cell>
    padding:[10,10,10,10]
    canvas.before:
        Color:
            rgba: app.theme_cls.accent_color
        Rectangle:
            pos: self.pos
            size: self.size
    size_hint_y:None
    size_hint_x:cell.size_hint_x
    height:dp(48)
    MDLabel:
        id:cell
        text:root.text               
''')

class Header(BoxLayout):
    text = StringProperty()



class Cell(BoxLayout):
    text = StringProperty()




class Table(BoxLayout):
    cols = NumericProperty(1)
    table_content = ListProperty([{"col 1":"row 11","col 2":"row 21"},{"col 1":"row 12","col 2":"row 22"}],allownone=True)
    thead = ListProperty()
    tbody = ListProperty()
    color = [128, 0, 2, 0.8]



    def __init__(self, **kwargs):
        super(Table, self).__init__(**kwargs)

        for i in self.table_content:
            self.thead =[]
            for j in i.keys():
                self.thead.append(j)
        self.ids['header'].cols = len(self.thead)
        self.ids['body'].cols = len(self.thead)
        for i in self.thead:
            head = Header(text=i.upper())
            self.ids['header'].add_widget(head)
        for i in self.table_content:
            for j in i.keys():
                body = Cell(text=i[j])
                self.ids['body'].add_widget(body)





#create MDTable.py file and save above code
#then import class where to add table widget
#simply use below function pass id where to add table and list of data
    def add_table(self,id,list):
        from table import Table
        id.add_widget(Table(table_content=list))
# list example:[{'head1':'content1','head2':'content2'},###{'head1':'content1','head2':'content2'}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2013-01-19
    • 2014-05-11
    相关资源
    最近更新 更多