KivyMD icon indicating copy to clipboard operation
KivyMD copied to clipboard

Unexpected behaviour of MDSegmentedButton

Open Sahil-pixel opened this issue 3 months ago • 0 comments

I am adding and removing widgets MDSegmentedButtonItem by button press but outcome is not as expected . MDSegmentedButtonItem increases its size , see video

Code and Logs


from kivymd.uix.label import MDLabel
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.screen import MDScreen
from kivymd.uix.segmentedbutton import (
    MDSegmentedButton,
    MDSegmentedButtonItem,
    MDSegmentButtonLabel,
)
from kivymd.app import MDApp

KV = '''
<MS>:
    md_bg_color: self.theme_cls.backgroundColor

    MDBoxLayout:
        id: box
        orientation: "vertical"
        size_hint_x: .7
        adaptive_height: True
        spacing: "24dp"
        pos_hint: {"center_x": .5, "center_y": .5}
        MDSegmentedButton:
            id:sb
        Button:
            text:'Add Items'
            size_hint:None,None
            pos_hint:{'x':0,'y':0}
            size:'80dp','48dp'
            on_release:root._add()
        Button:
            text:'Remove Items'
            size_hint:None,None
            size:'80dp','48dp'
            pos_hint:{'right':1,'y':0}
            on_release:root._remove()
'''

Builder.load_string(KV)
class MS(MDScreen):
    items=[]
    def _add(self):
        self.items=[]
        for i in range(4):
            item=MDSegmentedButtonItem(MDSegmentButtonLabel(text="Albums",),on_active=lambda x:print(x),)
            self.items.append(item)
            self.ids.sb.add_widget(item)
    
    def _remove(self):
        print('hello')

        for item in self.items:
            print(item)
            self.ids.sb.ids.container.remove_widget(item)


                        

class Example(MDApp):
        
    def build(self):
        return MS()


Example().run()

Screenshots

Add images to explain u

https://github.com/kivymd/KivyMD/assets/57060638/deeb704d-008f-41c1-beed-7f64d8d363a9

Versions

  • OS: ubuntu
  • Python: 3.10
  • Kivy: 2.3.0
  • KivyMD: 2.0.1dev

Sahil-pixel avatar Apr 23 '24 11:04 Sahil-pixel