popup: fixed popup usage when content is defined from kv + add example. closes #562

This commit is contained in:
Mathieu Virbel 2012-07-09 20:11:30 +02:00
parent 2392816909
commit 03a1978f3e
2 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,33 @@
'''
Example to show a Popup usage with the content from kv lang.
'''
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.app import App
from kivy.lang import Builder
Builder.load_string('''
<CustomPopup>:
size_hint: .5, .5
auto_dismiss: False
title: 'Hello world'
Button:
text: 'Click me to dismiss'
on_press: root.dismiss()
''')
class CustomPopup(Popup):
pass
class TestApp(App):
def build(self):
b = Button()
b.bind(on_press=self.show_popup)
return b
def show_popup(self, b):
p = CustomPopup()
p.open()
TestApp().run()

View File

@ -65,6 +65,7 @@ popup from closing by explictly returning True from your callback ::
'''
__all__ = ('Popup', 'PopupException')
from kivy.logger import Logger
from kivy.animation import Animation
@ -73,6 +74,13 @@ from kivy.properties import StringProperty, BooleanProperty, ObjectProperty, \
NumericProperty, ListProperty
class PopupException(Exception):
'''Popup exception, fired when multiple content are added to the popup.
.. versionadded:: 1.4.0
'''
class Popup(FloatLayout):
'''Popup class. See module documentation for more information.
@ -194,6 +202,14 @@ class Popup(FloatLayout):
window = Window
return window
def add_widget(self, widget):
if self._container:
if self.content:
raise PopupException('Popup can have only one widget as content')
self.content = widget
else:
super(Popup, self).add_widget(widget)
def open(self, *largs):
'''Show the popup window from the :data:`attach_to` widget. If set, it
will attach to the nearest window. If the widget is not attached to any
@ -222,7 +238,7 @@ class Popup(FloatLayout):
.. versionchanged:: 1.3.0
When the popup is dismissed, it will be faded out, before
When the popup is dismissed, it will be faded out, before
removal from the parent. If you don't want animation, use:
popup.dismiss(animation=False)