Popup在kivy中打开后,将焦点放在标签上

时间:2017-11-19 15:17:09

标签: python tabs kivy

我有一个带有一些标签的应用程序(我使用了tabpanel小部件)。我有一个选项卡,里面有一个Popup,我需要在弹出窗口打开后给标签设置焦点,这样我就可以移动到其他选项卡了。

我想知道如何做到这一点。

提前致谢!

1 个答案:

答案 0 :(得分:0)

不要在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

输出

Figure 1 - Tabbed Panel Figure 2 - Clicked tab4, Popup Opened Figure 3 - Any Click Outside Popup, Popup Closed Figure 4 - Clicked tab3

示例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