【问题标题】:Give focus to tab after Popup opens in kivy在 kivy 中打开 Popup 后将焦​​点放在选项卡上
【发布时间】:2017-11-19 15:17:08
【问题描述】:

我有一个带有一些标签的应用程序(我使用了 tabpanel 小部件)。我有一个选项卡,里面有一个弹出窗口,我需要在弹出窗口打开后将焦点放在该选项卡上,以便我可以移动到其他选项卡。

我想知道该怎么做。

提前致谢!

【问题讨论】:

  • 请提供您正在做的最小示例并描述想要的行为...

标签: python tabs kivy


【解决方案1】:

不要在 kv 文件中将弹出窗口添加为子项

Popup 是一个特殊的小部件。不要尝试将其作为子项添加到任何其他小部件。如果您这样做,Popup 将像普通小部件一样处理,不会隐藏在后台创建,您将无法关闭/关闭它。

TabbedPanelItem:
    text: 'tab4'
    RstDocument:
        text: root.text
    Popup:    # Don't add popup as a child in kv file
        title: "Popup"
        size_hint: None, None
        size: 250, 250
        BoxLayout:
            orientation: "vertical"
            Label:
                text:
                    "Popup content area"

最佳实践

在以下示例中,按下 tab4 时会出现一个弹出窗口。在示例 1 中,弹出消息框是使用 动态类 创建的。在示例 2 中,弹出消息框是使用 非动态类 创建的。弹出窗口外的任何点击都会关闭/关闭它,您可以移动到其他选项卡。详情请参考示例。

示例 1 - 使用动态类创建弹出窗口

main.py

​​>
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel


class TabbedPanelDemo(TabbedPanel):
    text = """
.. _top:

Hello world
===========

This is an **emphased text**, some ``interpreted text``.
And this is a reference to top_::

    $ print("Hello world")

"""


class TestApp(App):
    def build(self):
        return TabbedPanelDemo()


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

test.kv

#:kivy 1.10.0
#:import Factory kivy.factory.Factory

<MessageBox@Popup>:    # Creating a dynamic class
    title: "Popup"
    size_hint: None, None
    size: 250, 250
    BoxLayout:
        orientation: "vertical"
        Label:
            text:
                "Popup content area"


<TabbedPanelDemo>:
    size_hint: .5, .5
    pos_hint: {'center_x': .5, 'center_y': .5}
    do_default_tab: False

    TabbedPanelItem:
        text: 'first tab'
        Label:
            text: 'First tab content area'
    TabbedPanelItem:
        text: 'tab2'
        BoxLayout:
            Label:
                text: 'Second tab content area'
            Button:
                text: 'Button that does nothing'
    TabbedPanelItem:
        text: 'tab3'
        RstDocument:
            text:
                '\n'.join(("Hello world", "-----------",
                "You are in the third tab."))
    TabbedPanelItem:
        text: 'tab4'
        on_press:
            Factory.MessageBox().open()
        RstDocument:
            text: root.text

输出

示例 2 - 使用非动态类创建弹出窗口

main.py

​​>
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.popup import Popup


class MessageBox(Popup):
    pass


class TabbedPanelDemo(TabbedPanel):
    text = """
.. _top:

Hello world
===========

This is an **emphased text**, some ``interpreted text``.
And this is a reference to top_::

    $ print("Hello world")

"""

    def display_message_box(self):
        popup = MessageBox()
        popup.open()


class Test2App(App):
    def build(self):
        return TabbedPanelDemo()


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

test2.kv

#:kivy 1.10.0

<MessageBox>:
    title: "Popup"
    size_hint: None, None
    size: 250, 250
    BoxLayout:
        orientation: "vertical"
        Label:
            text:
                "Popup content area"


<TabbedPanelDemo>:
    size_hint: .5, .5
    pos_hint: {'center_x': .5, 'center_y': .5}
    do_default_tab: False

    TabbedPanelItem:
        text: 'first tab'
        Label:
            text: 'First tab content area'
    TabbedPanelItem:
        text: 'tab2'
        BoxLayout:
            Label:
                text: 'Second tab content area'
            Button:
                text: 'Button that does nothing'
    TabbedPanelItem:
        text: 'tab3'
        RstDocument:
            text:
                '\n'.join(("Hello world", "-----------",
                "You are in the third tab."))

    TabbedPanelItem:
        text: 'tab4'
        on_press:
            root.display_message_box()
        RstDocument:
            text: root.text

【讨论】:

  • 谢谢,但我仍然不明白如何创建类似弹出窗口或 ModalView 以便我仍然可以在选项卡之间移动。此外,我的弹出窗口由于某种原因永远不会关闭。
  • 我已将我的帖子更新为“不要弹出”,并提供了两个创建弹出窗口的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
相关资源
最近更新 更多