尝试从TabbedPanel中删除小部件时出现错误' self._childrens.pop(小部件,无)

时间:2015-11-25 18:40:05

标签: python kivy

我正在尝试从MainPanel中删除ScreenManager小部件,并将其替换为不同的ScreeManager小部件。但我收到上述错误。我已经能够以这种方式删除其他小部件(屏幕)并且从未出现过错误,但ScreenManagers似乎有所不同。

main.py

class MainPanel(TabbedPanel):
    manager = ObjectProperty()
    usb_manager = ObjectProperty()
    hubtests_sm = ObjectProperty()


    def __init__(self, *args, **kwargs):

        super(MainPanel, self).__init__(*args, **kwargs)



    def switch_sm(self):
        self.manager = self.hubtests_sm
        self.remove_widget(self.usb_manager)
        self.add_widget(self.hubtests_sm)

main.kv

<MainPanel>:
    id: mp
    manager: usb_manager
    usb_manager: usb_manager


    size_hint: 1,1
    tab_width: 65
    do_default_tab: False
    tab_pos: 'top_right'


    TabbedPanelItem:
        id: usb_tab
        text: 'Usb'
        BoxLayout:
            orientation: 'vertical'
            UsbScreenManager:
                id: usb_manager

usb_screen_manager.kv

#:import label kivy.uix.label
#:import sla kivy.adapters.simplelistadapter
#:import hvd hub_validation_dict
#:import hvt hub_validation_test2
#:import cfg config

<UsbScreenManager>:
    id: usb_manager 
    h2h: h2h
    hvs: hvs
    v9_icon: v9_icon
    v9_test: v9_test
    c5_icon: c5_icon
    c5_test: c5_test

    H2HScreen:
        id:h2h
    HubValidationScreen:
        id:hvs
    V9ValidationIconScreen
        id: v9_icon
    V9ValidationTestScreen:
        id: v9_test
    C5ValidationIconScreen:
        id: c5_icon
    C5ValidationTestScreen:
        id: c5_test

<H2HScreen>:
    id: h2h
    name: 'h2h'
    BoxLayout:
        id:bl
        orientation: 'vertical'
        TestIcon:
            id: h2hicon
            source: 'h2h.png'


<HubValidationScreen>:
    id: hvs
    name: 'hvs'


    BoxLayout:
        id:bl
        orientation: 'vertical'

        TestIcon:
            id: hubvicon

            source: 'usb_button.png'
            on_press: app.root.switch_sm()<----Attempt to switch screenmanager


  File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1465, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "usb_screen_manager.kv", line 53, in <module>
     on_press: app.root.switch_sm()
   File "sam1.py", line 211, in switch_sm
     self.remove_widget(self.usb_manager)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/tabbedpanel.py", line 547, in remove_widget
     self._childrens.pop(widget, None)
 TypeError: pop() takes at most 1 argument (2 given)

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,因为我误解了TabbedPanel类。当我应该从TabbedPanelItem类调用它时,我从MainPanel类调用remove_widget。所以我为MainPanel类中的tabbedpanel项创建了一个ObjectProperty,并用它来删除屏幕管理器小部件。但由于它们的加载方式,我最终不得不做一个clear_widgets,然后以正确的顺序重新添加小部件。