【问题标题】:Kivy TabbedPanel switch_to work inconsistentlyKivy TabbedPanel switch_to 工作不一致
【发布时间】:2020-04-05 08:11:45
【问题描述】:

我正在编写一个启动前端、运行后端然后加载前端的代码。前端由TabbedPanel组成,当前显示的选项卡可能会被后端改变。

这是 MRE:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader


def button(instance):

    instance.parent.parent.switch_to(instance.parent.parent.tab_2)  # accessing TabbedPanel without messing with sending
    # a variable


def backend(frontend):

    # this class represents main backend function. In the result of its execution there might be a need to switch to
    # another tab

    frontend.switch_to(frontend.tab_2)


class MyTabbedPanel(TabbedPanel):

    def __init__(self, **kwargs):

        super().__init__()
        self.tab_1 = TabbedPanelHeader()
        self.tab_2 = TabbedPanelHeader()
        self.tab_1.content = Button(text='Tab 1')
        self.tab_1.content.bind(on_release=button)
        self.tab_2.content = Label(text='Tab 2')
        self.add_widget(self.tab_1)
        self.add_widget(self.tab_2)


class Application(App):

    def build(self):
        frontend = MyTabbedPanel()
        backend(frontend)
        return frontend


Application().run()

我添加的用于比较的按钮,用于从选项卡 1 切换到选项卡 2 工作正常,但是启动应用程序时的自动切换不起作用。

有什么问题?提前谢谢你。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    在您调用backend 时,build 方法没有返回根小部件,更不用说切换到的选项卡了。
    解决此问题的一种方法是使用 Clock 模块在 build 结束后安排对 backend 的调用。

        def build(self):
            frontend = MyTabbedPanel()
            # backend(frontend)
            from functools import partial
            from kivy.clock import Clock
            Clock.schedule_once(partial(backend, frontend))
            return frontend
    

    您还必须向backend 方法添加一个args 参数,因为Clock 发送一个dt 值:
    def backend(frontend, *args):

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多