如何在启动时显示 KivyMD 数据表?

Posted

技术标签:

【中文标题】如何在启动时显示 KivyMD 数据表?【英文标题】:How can I show KivyMD datatable on start-up? 【发布时间】:2021-06-18 00:06:25 【问题描述】:

我正在制作一个将在选项卡内显示数据表的应用程序,并且我正在尝试在应用程序启动时显示它。

现在我正在使用一个按钮来显示数据表,但在完成的应用程序中,我希望它在启动时或打开选项卡时显示。

.py

from kivymd.app import MDApp
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from kivy.uix.widget import Widget
from kivymd.uix.datatables import MDDataTable
from kivy.uix.anchorlayout import AnchorLayout
from kivy.metrics import dp

Builder.load_file('testwidgets.kv')

class main(Widget):
    pass

class Tab(MDFloatLayout, MDTabsBase):
    def load_foodtable(self, *args):
        test = range(5)
        self.food_layout = AnchorLayout()
        self.food_tabel = MDDataTable(
            pos_hint='center_y': 0.5, 'center_x': 0.5,
            size_hint=(0.9, 0.6),
            column_data=[
                ("Name", dp(30)),
                ("Carbohydrates", dp(30)),
                ("Protin", dp(30)),
                ],
                row_data=[
                    ("stuff", "moreStuff", "2") for i in(test)
                ],
                elevation=20,
            )

        self.add_widget(self.food_tabel)
        return self.food_layout

    def on_enter(self):
        self.load_foodtable()



class DemoApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Teal"
        self.theme_cls.primary_hue = "400"
        return main()


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

.kv

<main>:

    MDNavigationLayout:
        size : root.width, root.height

        ScreenManager:
            id: screen_manager

            Screen:
                name: "mddatatable"

                BoxLayout:
                    orientation: 'vertical'

                    MDToolbar:
                        id: toolbar
                        title: "Drinks and Food"
                        elevation: 10

                    MDTabs:

                        Tab:
                            title: "Food"


                        Tab:
                            title: "Drink"

<Tab>:
    MDRaisedButton:
        text: 'open table'
        on_press: root.on_enter()
        pos_hint: 'center_x': .5, 'center_y': .5

【问题讨论】:

【参考方案1】:

我不知道它是否仍然与您相关,但我遇到了类似的问题,所以也许这个解决方案对任何人都有用......

您需要在.kv 中为MDTabs 指定on_tab_switch 事件:

on_tab_switch: app.on_tab_switch(*args)

您还需要在.pyDemoApp 类中创建此方法,以将调用转发到您的on_enter 函数:

def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
    instance_tab.on_enter()

您的整个代码应如下所示: .py

from kivymd.app import MDApp
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from kivy.uix.widget import Widget
from kivymd.uix.datatables import MDDataTable
from kivy.uix.anchorlayout import AnchorLayout
from kivy.metrics import dp

Builder.load_file('testwidgets.kv')

class main(Widget):
    pass

class Tab(MDFloatLayout, MDTabsBase):
    def load_foodtable(self, *args):
        test = range(5)
        self.food_layout = AnchorLayout()
        self.food_tabel = MDDataTable(
            pos_hint='center_y': 0.5, 'center_x': 0.5,
            size_hint=(0.9, 0.6),
            column_data=[
                ("Name", dp(30)),
                ("Carbohydrates", dp(30)),
                ("Protin", dp(30)),
                ],
                row_data=[
                    ("stuff", "moreStuff", "2") for i in(test)
                ],
                elevation=20,
            )

        self.add_widget(self.food_tabel)
        return self.food_layout

    def on_enter(self):
        self.load_foodtable()



class DemoApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Teal"
        self.theme_cls.primary_hue = "400"
        return main()

    def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
        instance_tab.on_enter()


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

.kv

<main>:

    MDNavigationLayout:
        size : root.width, root.height

        ScreenManager:
            id: screen_manager

            Screen:
                name: "mddatatable"

                BoxLayout:
                    orientation: 'vertical'

                    MDToolbar:
                        id: toolbar
                        title: "Drinks and Food"
                        elevation: 10

                    MDTabs:
                        on_tab_switch: app.on_tab_switch(*args)
                        Tab:
                            title: "Food"


                        Tab:
                            title: "Drink"

<Tab>:
    MDRaisedButton:
        text: 'open table'
        on_press: root.on_enter()
        pos_hint: 'center_x': .5, 'center_y': .5

【讨论】:

以上是关于如何在启动时显示 KivyMD 数据表?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 kivymd 功能开始时显示加载屏幕?

仅在 kivy 首次启动时显示设置屏幕

如何在应用启动和退出时显示 Admob 插页式广告

如何避免在后台运行时启动应用程序时显示启动画面?

如何在应用程序启动时显示锁定屏幕?

如何在 Emacs 启动时显示 Org 模式议程?