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!
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"
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.
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()
#: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
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()
#: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.