Created
September 17, 2014 21:32
-
-
Save kived/2d900d855efb16ddbe59 to your computer and use it in GitHub Desktop.
Kivy: asynchronous popups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kivy | |
kivy.require('1.8.0') | |
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.uix.popup import Popup | |
from twisted.internet import defer | |
class DeferredPopup(Popup): | |
def __init__(self, **kwargs): | |
super(DeferredPopup, self).__init__(**kwargs) | |
self.d = None | |
def show(self, *args, **kwargs): | |
self.d = defer.Deferred() | |
self.open(*args, **kwargs) | |
return self.d | |
def on_ok(self): | |
try: | |
result = self.get_result() | |
except Exception: | |
return | |
self.d.callback(result) | |
self.dismiss() | |
def on_cancel(self): | |
self.d.callback(None) | |
self.dismiss() | |
def get_result(self): | |
raise NotImplementedError | |
class MyPopup(DeferredPopup): | |
def get_result(self): | |
return True | |
root = Builder.load_string(''' | |
<MyPopup>: | |
BoxLayout: | |
orientation: 'vertical' | |
Label: | |
text: 'Hello!' | |
BoxLayout: | |
size_hint_y: None | |
height: sp(72) | |
Button: | |
text: 'OK' | |
on_press: root.on_ok() | |
Button: | |
text: 'Cancel' | |
on_press: root.on_cancel() | |
BoxLayout: | |
Button: | |
text: 'Open Popup' | |
on_press: app.open_popup() | |
''') | |
class TestApp(App): | |
def build(self): | |
return root | |
@defer.inlineCallbacks | |
def open_popup(self): | |
print 'open popup' | |
p = MyPopup() | |
result = yield p.show() | |
print 'popup closed' | |
print 'result:', result | |
if __name__ == '__main__': | |
from kivy.support import install_twisted_reactor | |
install_twisted_reactor() | |
TestApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment