Switching “tab” in accordion programmatically Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Switching “tab” in accordion programmatically without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I want to switch “tab” when the Load file button has been pressed on a file, meaning that when a file has been loaded it should switch to the second AccordionItem. How can I do this programmatically?

 42     Accordion:                                                                  
 43         orientation: 'horizontal'                                               
 44         anim_duration: 2                                                        
 45                                                                                 
 46         AccordionItem:                                                          
 47             title: 'Load File'.title()                                          
 48             Page:                                                               
 49                 Header:                                                         
 50                     text: 'Load File'.title()                                   
 51                 Body:                                                           
 52                     BoxLayout:                                                  
 53                         orientation: 'vertical'                                 
 54                                                                                 
 55                         FileChooser:                                                                                                                                                              
 56                             id: _filechooser                                    
 57                             dirselect: False                                    
 58                             multiselect: False                                  
 59                             show_hidden: False                                  
 60                             path: '~/'                                          
 61                             filters: ['*.[Zz][Ii][Pp]', '*.[Xx][Mm][Ll]']       
 62                             FileChooserIconLayout                               
 63                                                                                 
 64                         Button:                                                 
 65                             height: '48dp'                                      
 66                             size_hint_y: None                                   
 67                             text: 'Load'.title()                                
 68                             on_release: root.load(_filechooser.path, _filechooser.selection)
 69                                                                                 
 70                 Footer:                                                         
 71                     text: 'Choose file to load…'                                
 72                                                                                 
 73         AccordionItem:                                                          
 74             title: 'Dmarc Report'.title()                                       
 75             Page:                                                               
 76                 Header:                                                         
 77                     text: 'Dmarc Report'.title()                                
 78                 Body:                                                           
 79                 Footer:                                                         
 80                     text: 'Viewing currently loaded file…'

Answer

You can use dispatch() method to simulate clicking on AccordionItem:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string('''
<MyWidget>:
    orientation: "vertical"
    Button:
        text: "switch"
        size_hint: 1, 0.1
        on_press:
            if item1.collapse: item1.dispatch('on_touch_down', item1)
            else: item2.dispatch('on_touch_down', item2)
    Accordion:
        AccordionItem:
            id: item1
            title: "1"
            Label:
                text: "1"
        AccordionItem:
            id: item2
            title: "2"
            Label:
                text: "2"
''')


class MyWidget(BoxLayout):
    pass

class MyApp(App):
    def build(self):        
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()
We are here to answer your question about Switching “tab” in accordion programmatically - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji