Python Kivy标签面板。如何从另一个类调用switch_?

Python Kivy标签面板。如何从另一个类调用switch_?,python,kivy,Python,Kivy,在下面的代码中,我需要使用编辑按钮选项卡上的按钮切换到编辑输入选项卡。我真的需要这样切换,因为我需要在预定义类EditButton和EditInput之间切换。这是一个较大程序的一部分,在布局的不同位置有几个按钮,我无法在类中定义它们。我尝试了很多方法调用switch\u to(引用中的示例),但都不起作用 代码 from kivy.animation import Animation from kivy.app import App from kivy.clock import Clock f

在下面的代码中,我需要使用
编辑按钮选项卡上的按钮切换到
编辑输入选项卡
。我真的需要这样切换,因为我需要在预定义类
EditButton
EditInput
之间切换。这是一个较大程序的一部分,在布局的不同位置有几个
按钮,我无法在
类中定义它们。我尝试了很多方法调用
switch\u 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()
  • 使用
    App.get\u running\u App()
    获取应用程序的实例
  • 使用
    root
    获取根目录的实例
  • 片段 笔记
    • 在kv中,您定义了一个动态类,
      。这应该是一个阶级规则,
      因为在Python代码中,您已经定义了
      类MainTabbedPanel(TabbedPanel):
      即继承不匹配和 类类型不匹配

    当我将
    mtp.ids.edit\u input
    更改为
    mtp.ids.edit\u input\u tab
    后,它会工作。令人惊叹的
    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])
    
    def change_tab(self, tab):
        print('TAB', tab)
        mtp = App.get_running_app().root
        mtp.switch_to(mtp.ids.edit_input_tab)