From 03a1978f3e700d414b665efd64ad94812450d166 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Mon, 9 Jul 2012 20:11:30 +0200 Subject: [PATCH] popup: fixed popup usage when content is defined from kv + add example. closes #562 --- examples/widgets/popup_with_kv.py | 33 +++++++++++++++++++++++++++++++ kivy/uix/popup.py | 18 ++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 examples/widgets/popup_with_kv.py diff --git a/examples/widgets/popup_with_kv.py b/examples/widgets/popup_with_kv.py new file mode 100644 index 000000000..61bf7ca8d --- /dev/null +++ b/examples/widgets/popup_with_kv.py @@ -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(''' +: + 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() diff --git a/kivy/uix/popup.py b/kivy/uix/popup.py index e0a2994d8..a5571c264 100644 --- a/kivy/uix/popup.py +++ b/kivy/uix/popup.py @@ -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)