Search code examples
pythonpython-3.xkivykivy-languagekivymd

Why am I getting double labels?


I am trying to make a swiper widget that contains a list of items but I keep getting double labels. I have really been messing around with different layouts to see if that changes as well as how I insert the swiperitems. I cant find anything online with someone having a similiar issue. I'm new to kivy so I'm sure its prob something simple

This is my python file.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget

from kivymd.app import MDApp
from kivymd.uix.list import TwoLineListItem
from kivymd.uix.swiper.swiper import MDSwiperItem

class MySwiperItem(MDSwiperItem):
    pass

class DemoApp(MDApp):
    
    def build(self):
        self.theme_cls.theme_style = "Dark"
        return Builder.load_file('demo.kv')
    
    
    def on_start(self):

        SwiperTest = MySwiperItem()

        SwiperTest.ids.header.text = "test"
        SwiperTest.ids.list.add_widget(
            TwoLineListItem(
                text = "text1",
                secondary_text = "text2"
            )
        )

        self.root.ids.swiper.add_widget(
            SwiperTest
        )

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

and this is my kivy file.

<DrawerClickableItem@MDNavigationDrawerItem>
    focus_color: "#e7e4c0"
    text_color: "#4a4939"
    icon_color: "#4a4939"
    ripple_color: "#c5bdd2"
    selected_color: "#0c6c4d"


<DrawerLabelItem@MDNavigationDrawerItem>
    text_color: "#4a4939"
    icon_color: "#4a4939"
    focus_behavior: False
    selected_color: "#4a4939"
    _no_ripple_effect: True

<MySwiperItem>:
    orientation: "vertical"            
    MDLabel:
        id: header
        font_style: "H5"
        size_hint_y: None
        height: self.texture_size[1] + dp(40)
        pos_hint: {"center_x": .5, "top": 1}
        md_bg_color: "grey"

    MDScrollView:
        MDList:
            id: list
                

MDScreen:
    MDNavigationLayout:

        MDScreenManager:

            MDScreen:
                orientation: "vertical"

                MDTopAppBar:
                    id: nav_bar
                    title: "Smart Indoor Watering System"
                    elevation: 4
                    pos_hint: {"top": 1}
                    md_bg_color: "#e7e4c0"
                    specific_text_color: "#4a4939"
                    left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

                
                MDSwiper:
                    id: swiper
                    size_hint_y: None
                    height: root.height - nav_bar.height - dp(40)
                    y: root.height - self.height - nav_bar.height - dp(20)


        MDNavigationDrawer:
            id: nav_drawer
            radius: (0, 16, 16, 0)

            MDNavigationDrawerMenu:

                MDNavigationDrawerHeader:
                    title: "Header title"
                    title_color: "#4a4939"
                    text: "Header text"
                    spacing: "4dp"
                    padding: "12dp", 0, 0, "56dp"

                MDNavigationDrawerLabel:
                    text: "Mail"

                DrawerClickableItem:
                    icon: "gmail"
                    right_text: "+99"
                    text_right_color: "#4a4939"
                    text: "Inbox"

                DrawerClickableItem:
                    icon: "send"
                    text: "Outbox"

                MDNavigationDrawerDivider:

                MDNavigationDrawerLabel:
                    text: "Labels"

                DrawerLabelItem:
                    icon: "information-outline"
                    text: "Label"

                DrawerLabelItem:
                    icon: "information-outline"
                    text: "Label"


Solution

  • Your demo.kv is being loaded twice. In addition to your explicit Builder.load_file(), the kv file is also loaded automatically by the App. See the documentation. Try changing the name of the demo.kv to something unrelated to the App name.