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?
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 *
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'
# 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)
background(0, 0, 0) # black background for screen
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'
# callback function for "Hi" button
def hi_pressed(self, sender):
a = self.alert()
if a == 1:
a = console.alert('Alert', 'Boo!', 'OK')
sv = SceneView()
sv.scene = MyScene()