Kivy选项卡面板。如何从另一个类调用切换到?

2024-06-16 09:34:03 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,我需要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()

编辑

我试过用下面的片段。它打印IDSMainTabbedPanel,但不更改制表符

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])

Tags: tofromimportselfinputinitdefedit
1条回答
网友
1楼 · 发布于 2024-06-16 09:34:03
  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):即继承不匹配和 类类型不匹配

相关问题 更多 >