选项卡kivy / KivyMD中的ScrollView

时间:2020-08-14 10:13:20

标签: python kivy kivymd

问题已更新

我有一个代码:

from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase

KV = '''
BoxLayout:
    orientation: "vertical"

    MDToolbar:
        left_action_items: [["menu", lambda x: x]]
        title: "Smart home & its components"

    MDTabs:
        id: tabs
        on_tab_switch: app.on_tab_switch(*args)
        Tab:
            text: 'Smart home'
            FitImage:
                source: 'smarthome.png'
        Tab:
            text: 'Smart garage'
            ScrollView:
                do_scroll_x: False
                FitImage:
                    size_hint: (None, None)
                    size: root.size
                    source: 'garage.jpg'
                Button:
                    size_hint: (None, None)
                    size: (100, 100)
                Button:
                    size_hint: (None, None)
                    size: (100, 100)


<Tab>:
'''


class Tab(FloatLayout, MDTabsBase):
    pass


class Example(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
        pass


Example().run()

'''


class Tab(FloatLayout, MDTabsBase):
    pass


class Example(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
        pass


Example().run()

部分代码:

Button:
    size_hint: (None, None)
    size: (100, 100)
Button:
    size_hint: (None, None)
    size: (100, 100)
            

当我运行不带此部分的代码时,我在第二个选项卡中获得了可滚动的图像,但是当我运行带有此部分代码的代码时,便出现了错误。
错误:

Exception: ScrollView accept only one widget

我理解为什么会出错,并且ScrollView仅接受一个小部件,但是如果可能的话,如何将Buttons放置在可滚动图像的下面?
谢谢你

2 个答案:

答案 0 :(得分:1)

ScrollView:
    do_scroll_x: False
    
    MDList:
    
        FitImage:
            size_hint: (None, None)
            size: root.size
            source: 'garage.jpg'
        Button:
            size_hint: (None, None)
            size: (100, 100)
        Button:
            size_hint: (None, None)
            size: (100, 100)

答案 1 :(得分:0)

from kivy.lang import Builder
from kivy.properties import StringProperty

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.tab import MDTabsBase

KV = '''
MDBoxLayout:
    orientation: "vertical"

    MDToolbar:
        title: "Example Tabs Toolbar"

    MDTabs:
        id: tabs
        on_tab_switch: app.on_tab_switch(*args)
        
        Tab:
            text: 'Smart home'
            source: 'image.png'
                
        Tab:
            text: 'Smart garage'
            source: 'image.png'

<Tab>:
    orientation: "vertical"

    FitImage:
        source: root.source

    ScrollView:
    
        MDList:
            id: box

'''


class Tab(MDBoxLayout, MDTabsBase):
    source = StringProperty()


class Example(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_start(self):
        self.root.ids.tabs.switch_tab('Smart garage')

    def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
        instance_tab.ids.box.clear_widgets()
        for i in range(20):
            instance_tab.ids.box.add_widget(
                MDLabel(text=f"Text {i}", halign="center")
            )


Example().run()