On my Scene is on the left the „x“ to leave, how can I make a dialog that if I press the „x“ there come a dialog: Are you sure to leave?
Forum Archive
Make a dialog if I press the „x“
@Python567, you would need to run the scene with a custom ui.View, and define that view’s will_close method to show the dialog.
Or, it might be that, using a custom view, you need to provide your own ”X” on the screen, I do not remember. If that was the case, then you can show the dialog when handling the touch on the ”X”.
@Python567 scene has a method for that
from scene import *
class MyScene (Scene):
def setup(self):
pass
def stop(self):
print('x has been pressed')
run(MyScene())
@cvp, but does stop have an option to cancel the close?
@mikael I don't know, perhaps no. To be tried.
Édit: no option to cancel close, thus not the right solution.
@Python567 try
from scene import *
import console
import ui
class MyScene (Scene):
def setup(self):
w,h = ui.get_screen_size()
self.view.add_subview(ui.Button(frame=(w-32,0,32,32),action=self.close))
def close(self,sender):
if console.alert('close scene?','','yes','no',hide_cancel_button=True) == 1:
self.view.close()
run(MyScene())
As you see, button does not have title, so you only see the 'x' closing the scene.
@cvp, nice!
Below a slightly expanded version that shows how you can make this work even if the device is rotated by using flex, and how you can pause the scene while the dialog is open.
from scene import *
import console
import ui
class MyScene (Scene):
def setup(self):
w,h = ui.get_screen_size()
self.view.add_subview(
ui.Button(frame=(w-64,0,64,64),
flex='LB',
action=self.close))
SpriteNode(
'spc:PlayerShip1Orange',
parent=self,
position = self.size / 4,
).run_action(
Action.repeat_forever(
Action.rotate_by(.5))
)
def close(self,sender):
self.paused = True
if console.alert('close scene?','','yes','no',hide_cancel_button=True) == 1:
self.view.close()
self.paused = False
run(MyScene())
@mikael more than nice, sometimes i wonder what i'm doing here
@cvp, you are being polite, it was your idea.
@mikael I have first tried to change target action of close UIButton, possible but not so easy because some consequences.
@cvp @mikael Thanks a lot, that‘s awesome, but can I also create my Scene with ui or must I have it with the module „Scene“
That here is my code, but it doesn‘t work really:
```
import ui
import console
from scene import *
class Scene(ui.View):
def init(self):
self.background_color = 'gray'
self.name = 'Test'
#def close(self,sender):
self.paused = True
if console.alert('Close Scene?','','Yes','No',hide_cancel_button=True) == 1:
self.view.close()
self.paused = False
s = Scene()
s.present('fullscreen')
@Python567 why is your Scene an ui.View? See @mikael 's code
@Python567 if you want to use an ui.View, try
import ui
import console
from scene import *
class Scene(ui.View):
def __init__(self):
self.background_color = 'gray'
self.name = 'Test'
def will_close(self):
#self.paused = True
if console.alert('Close Scene?','','Yes','No',hide_cancel_button=True) == 2:
return
#self.paused = False
s = Scene()
s.present('fullscreen')
@cvp the code from @mikael is with with the Scene module, but I already have all of my Programm with ui, so...
@cvp if I click „no“ on your code, the skript will close completely!
@Python567 you're right. Shame on me. I forgot that, like scene.stop, view.will_close allow to add process at end but not to avoid closing. Thus, you have to present without title and to build your own close button
@Python567
import ui
import console
class Scene(ui.View):
def __init__(self):
self.background_color = 'gray'
self.name = 'Test'
self.add_subview(ui.Button(frame=(2,12,32,32),title='x',action=self.bclose))
def bclose(self,sender):
if console.alert('Close Scene?','','Yes','No',hide_cancel_button=True) == 2:
return
self.close()
s = Scene()
s.present('fullscreen', hide_title_bar=True)
@cvp thanks