在下面的代码中,我需要edit_button_tab上的按钮才能切换到edit_input_tab。我确实需要以这种方式进行切换,因为我需要在预定义的类EditButtonEditInput之间进行切换。这是一个较大的程序的一部分,在布局的不同位置几乎没有Buttons,我无法在<MainTabbedPanel>类中定义它们。我尝试了许多方法来调用switch_to(引号中的示例),但是它们没有用。

from kivy.animation import Animation
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelStrip
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader
from kivy.factory import Factory

theRoot = """
#:import Factory kivy.factory.Factory

<EditButton>
    orientation: 'vertical'
    Button:
        text: 'Switch to Edit Screen'
        on_press: root.change_tab('edit_screen')

<EditInput>
    orientation: 'vertical'
    TextInput:

<UnCloseableHeader>
    color: 0,0,0,1

    disabled_color: self.color
    # variable tab_width
    text: 'tabx'
    size_hint_x: None
    width: self.texture_size[0] + 40
    BoxLayout:
        pos: root.pos
        size_hint: None, None
        size_y: 20
        padding: 3
        Label:
            id: lbl
            text: root.text

<MainTabbedPanel@BoxLayout>
    size_hint: (1, 1)

    default_tab: edit_button_tab
    tab_width: 130
    FloatLayout:
        EditButton:
            id: edit_button
        EditInput:
            id: edit_input

    UnCloseableHeader:
        id: edit_button_tab
        text: 'Edit'
        content: edit_button.__self__

    UnCloseableHeader:
        id: edit_input_tab
        text: 'Edit Tab'
        content: edit_input.__self__

MainTabbedPanel:    

"""

class EditInput(BoxLayout):
    def __init__(self, **kwargs):
        super(EditInput, self).__init__(**kwargs)

class EditButton(BoxLayout):
    def __init__(self, **kwargs):
        super(EditButton, self).__init__(**kwargs)

    def change_tab(self, tab):
        print('TAB', tab)
        #call switch method from MainTabbedPanel
        '''the way I've tried
        mtp = MainTabbedPanel
        mtp.switch_to('edit_input_tab')'''


class MainTabbedPanel(TabbedPanel):

    def __init__(self, **kwargs):
        super(MainTabbedPanel, self).__init__(**kwargs)

    def switch(self, tab):
        print("SWITCH TO", tab, self.ids.keys())
        self.switch_to(self.ids[tab])

class UnCloseableHeader(TabbedPanelHeader):
    pass

Factory.register('UnCloseableHeader', cls=UnCloseableHeader)


sm = Builder.load_string(theRoot)

class TabbedPanelApp(App):
    def build(self):
        return sm

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

编辑

我已经尝试过以下代码段。它打印MainTabbedPanelIDS,但不更改选项卡。

class EditButton(BoxLayout):
    def __init__(self, **kwargs):
        super(EditButton, self).__init__(**kwargs)

    def change_tab(self, tab):
        print('TAB', tab)
        MainTabbedPanel.tab = tab
        MainTabbedPanel()
        #call switch method from MainTabbedPanel
        '''the way I've tried
        mtp = MainTabbedPanel
        mtp.switch_to('edit_input_tab')'''

class MainTabbedPanel(TabbedPanel):
    tab = ''
    def __init__(self, **kwargs):
        super(MainTabbedPanel, self).__init__(**kwargs)
        self.tabs_showing = True
        if self.tab != '':
            Clock.schedule_once(self.switch)

    def switch(self, dt):
        print("SWITCH TO", self.tab, self.ids.keys())
        self.switch_to(self.ids[self.tab])
分析解答
  1. 使用App.get_running_app()获取您的应用实例
  2. 使用root获取您的根实例

片段

def change_tab(self, tab):
    print('TAB', tab)
    mtp = App.get_running_app().root
    mtp.switch_to(mtp.ids.edit_input_tab)

笔记

  • 在您的kv中,您定义了一个动态类, <MainTabbedPanel@BoxLayout>。应该是阶级规则 <MainTabbedPanel>,因为您已经在Python代码中定义了 class MainTabbedPanel(TabbedPanel):,即继承不匹配和 类类型不匹配。