omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    Make a dialog if I press the „x“

    Pythonista
    3
    19
    3660
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Python567
      Python567 last edited by

      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?

      mikael cvp 3 Replies Last reply Reply Quote 0
      • mikael
        mikael @Python567 last edited by

        @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”.

        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @Python567 last edited by

          @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())
          
          mikael 1 Reply Last reply Reply Quote 1
          • mikael
            mikael @cvp last edited by

            @cvp, but does stop have an option to cancel the close?

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @mikael last edited by cvp

              @mikael I don't know, perhaps no. To be tried.

              Édit: no option to cancel close, thus not the right solution.

              1 Reply Last reply Reply Quote 0
              • cvp
                cvp @Python567 last edited by cvp

                @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.

                mikael 1 Reply Last reply Reply Quote 0
                • mikael
                  mikael @cvp last edited by

                  @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())
                  
                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @mikael last edited by

                    @mikael more than nice, sometimes i wonder what i'm doing here

                    mikael 1 Reply Last reply Reply Quote 0
                    • mikael
                      mikael @cvp last edited by

                      @cvp, you are being polite, it was your idea.

                      cvp 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @mikael last edited by

                        @mikael I have first tried to change target action of close UIButton, possible but not so easy because some consequences.

                        1 Reply Last reply Reply Quote 0
                        • Python567
                          Python567 last edited by Python567

                          @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“

                          1 Reply Last reply Reply Quote 0
                          • Python567
                            Python567 last edited by

                            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')
                            cvp 2 Replies Last reply Reply Quote 0
                            • cvp
                              cvp @Python567 last edited by

                              @Python567 why is your Scene an ui.View? See @mikael 's code

                              1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @Python567 last edited by

                                @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')
                                
                                1 Reply Last reply Reply Quote 0
                                • Python567
                                  Python567 last edited by

                                  @cvp the code from @mikael is with with the Scene module, but I already have all of my Programm with ui, so...

                                  1 Reply Last reply Reply Quote 0
                                  • Python567
                                    Python567 last edited by Python567

                                    @cvp if I click „no“ on your code, the skript will close completely!

                                    cvp 2 Replies Last reply Reply Quote 0
                                    • cvp
                                      cvp @Python567 last edited by

                                      @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

                                      1 Reply Last reply Reply Quote 0
                                      • cvp
                                        cvp @Python567 last edited by cvp

                                        @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)
                                        
                                        1 Reply Last reply Reply Quote 0
                                        • Python567
                                          Python567 last edited by

                                          @cvp thanks

                                          1 Reply Last reply Reply Quote 0
                                          • First post
                                            Last post
                                          Powered by NodeBB Forums | Contributors