简体   繁体   中英

Give focus to tab after Popup opens in kivy

I have an app with some tabs(i used tabpanel widget). I have a tab which has a Popup inside it, and I need to give the tab the focus after the Popup opens so I could move to other tabs.

I would like to know how to do that.

Thanks in advance!

Don't Add Popup As A Child In kv File

Popup is a special widget. Don't try to add it as a child to any other widget. If you do, Popup will be handled like an ordinary widget and won't be created hidden in the background and you won't be able to dismiss/close it.

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"

Best Practice

In the following example, a popup appears when tab4 is pressed. In Example 1 , the popup MessageBox is created using a dynamic class . In Example 2 , the popup MessageBox is created using a non-dynamic class . Any click outside the popup will dismiss/close it, and you could move to other tabs. Please refer to the example for details.

Example 1 - Create Popup Using Dynamic Class

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

Output

图1-选项卡式面板 图2-单击选项卡4,打开了弹出窗口 图3-外部弹出窗口中的任何单击,关闭的弹出窗口 图4-单击的选项卡3

Example 2 - Create Popup Using Non Dynamic Class

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM