切换" tab"手风琴以编程方式

时间:2016-05-27 12:49:54

标签: python kivy

我想切换" tab"当在文件上按下加载文件按钮时,意味着当文件加载时,它应该切换到第二个AccordionItem。我该如何以编程方式执行此操作?

 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…'

1 个答案:

答案 0 :(得分:1)

您可以使用dispatch()方法模拟点击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()