Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
console.alert with SceneView
-
So I'm trying to learn the ins and outs of Scene, SceneView, ui, etc., and the kinds of things that can be done with them, especially combining elements from the ui and console modules with the Scene functionality. I haven't been able to find many examples of this sort of thing, so in this test script (which started as one of the examples from the Pythonista docs), I'm trying to do the following:
- Create a SceneView with a simple scene (rectangle with image inside it).
- Add a touch function so when you touch anywhere, it pops up a ui.Button in the center of the image created above.
- When you press the new button, it pops up a console.hud_alert() box in acknowledgement.
- After that acknowledgement, it sleeps a few seconds and then pops up a console.alert() box and shows the result as a console.hud_alert().
I got the first three items above working, and the alert box does come up, but then things get weird: pressing either of the buttons on the alert box has no effect, nor does anything else -- in fact, I can't even exit the application; I have to completely close and reopen Pythonista. The code is below.
Why does it freeze this way? Is there something I'm missing about the interaction between the console functions and the ui/Scene ones?
Thanks!
Edit: Based on this post: https://omz-forums.appspot.com/pythonista/post/5301659244691456, I tried using ui.delay() on the call to self.alert; I had to add the @ui.background decorator on the self.alert() method, but the end result was the same as before: the alert box popped up but then froze everything.
from scene import * import ui import console import time class MyScene(Scene): def setup(self): self.root_layer = Layer(self.bounds) # Draw a 200px square with a white background and an image self.layer = Layer(Rect(self.size.w * 0.5 - 100, self.size.h * 0.5 - 100, 200, 200)) self.layer.background = Color(1, 1, 1) # white background for square self.layer.image = 'Cat_Face' self.add_layer(self.layer) # Grow square to twice its size self.layer.animate('scale_x', 2.0, duration=1.0, autoreverse=False) self.layer.animate('scale_y', 2.0, duration=1.0, autoreverse=False) def draw(self): background(0, 0, 0) # black background for screen self.root_layer.update(self.dt) self.root_layer.draw() def touch_began(self, touch): # Add a new button when screen is touched anywhere b = ui.Button() b.title = 'Hi' b.action = self.hi_pressed b.center = (self.layer.frame.x + self.layer.frame.w/2, self.layer.frame.y + self.layer.frame.h/2) b.width = 30 b.height = 20 b.flex = 'LRTB' b.background_color = 'white' b.tint_color = 'black' sv.add_subview(b) # callback function for "Hi" button @ui.in_background def hi_pressed(self, sender): console.hud_alert('Hi yourself!') time.sleep(3) a = self.alert() if a == 1: console.hud_alert('Got OK') else: console.hud_alert('Got Cancel') def alert(self): try: a = console.alert('Alert', 'Boo!', 'OK') return a except KeyboardInterrupt: return -1 sv = SceneView() sv.scene = MyScene() sv.present('full_screen')