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.


    Problem when running a ui program from shortcut.

    Pythonista
    shortcut home screen ico ui.view
    4
    17
    4386
    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.
    • satsuki.kojima
      satsuki.kojima last edited by

      I put a home screen icon with shortcut to run a program I made. The program is with a ui view and the problem is that when I leave the view after using it without closing it (which we usually do when using a tablet,) and next time I run a program from the shortcut, a new view is invoked over the same view I opened last time. That seems natural because shortcut is only to open a program and run it, but I wonder if there is any way to avoid opening as many views as I click the icon. It seems difficult to manage a program running in a different thread, but as I believe we should mostly face the same problem when making a shortcut, does anyone have good idea on it?

      cvp 2 Replies Last reply Reply Quote 0
      • cvp
        cvp @satsuki.kojima last edited by

        @satsuki.kojima usually, we close the view before swapping to another app

        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @satsuki.kojima last edited by cvp

          @satsuki.kojima this seems to work, automatic close of the view if app goes in background:

          import console
          import ui
          import threading
          import time
          
          class my_thread(threading.Thread):
          	global main_view
          	def __init__(self):
          		threading.Thread.__init__(self)
          	def run(self):
          		while True:
          			time.sleep(1)
          			if console.is_in_background():
          				main_view.close()
          				break
          
          main_view = ui.View()
          main_view.background_color = 'white'
          th = my_thread()
          th.start()
          main_view.present()
          
          satsuki.kojima 1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            The discussion here might help with some ideas:

            https://forum.omz-software.com/topic/5440/prevent-duplicate-launch-from-shortcut/17

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

              @JonB Not sure he wants to prohibit a second launch. As I understood (perhaps erroneously), he wants to restart its script from scratch but the previous view is still there.

              JonB 1 Reply Last reply Reply Quote 0
              • satsuki.kojima
                satsuki.kojima @cvp last edited by

                @cvp

                Yeah, this seems to work somehow, I mean I’m not quite sure why it works.
                Anyway, adding your codes, I run my program by shortcut, leave it without closing it, once again I click the shortcut and now the previous view is closed. But this behavior is not stable, sometimes the view is still running behind the new view. I’m trying to find what makes this difference of behavior.

                So far, by adding some print statement as follows:

                class my_thread(threading.Thread):
                global main_view

                def __init__(self):
                	self.timestamp = time.time()
                	threading.Thread.__init__(self)
                	
                def run(self):
                	while True:
                		time.sleep(1)
                		print(self.timestamp, console.is_in_background())
                		if console.is_in_background():
                			print('closing view')
                			main_view.close()
                			break
                

                Seems like my_thraead closes the view only when the Pythonista itself is re-opened.
                For example when I run this program on Pythonista (not from shortcut,) close the view and then again run the program, the output in the console shows like this:

                1618751761.064212 False
                1618751765.282424 False
                1618751761.064212 False
                1618751765.282424 False
                1618751761.064212 False
                1618751765.282424 False
                ;
                ;
                (meaning two threads are still running)

                and when I go back to the home screen and reopen Pythonista, the output shows like this:

                1618751761.064212 True
                closing view
                1618751765.282424 True
                closing view

                I’m not sure but it may make sense. What do you think?
                Still, I have some mysterious behavior so keep working on it. Thank you for your help. I didn’t think about using “console.is_in_background()”.

                cvp 2 Replies Last reply Reply Quote 0
                • cvp
                  cvp @satsuki.kojima last edited by

                  @satsuki.kojima do you see a difference if you have or not a tab with your script open?

                  satsuki.kojima 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @satsuki.kojima last edited by

                    @satsuki.kojima my solution is correct, I think, if you leave Pythonista by opening another app, like you asked.

                    1 Reply Last reply Reply Quote 0
                    • satsuki.kojima
                      satsuki.kojima @cvp last edited by

                      @cvp
                      I closed all the tab of my script, and now it works all fine so far.

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

                        @cvp I think the singleton view approach in that thread would work, with modification -- basically as part of the launch, it would close and reopen the view ( or reinitialize it). Haven't tried it myself.

                        cvp satsuki.kojima 2 Replies Last reply Reply Quote 0
                        • cvp
                          cvp @JonB last edited by

                          @JonB good idea. @satsuki-kojima you could try

                          1 Reply Last reply Reply Quote 0
                          • satsuki.kojima
                            satsuki.kojima @JonB last edited by

                            @JonB
                            Excuse my slow response. I knew, advised by you, that the same discussion was held in this forum 2 years ago. There were many ideas in that discussion as well although I haven't been all through this yet.
                            I am very new to singleton approach and my understanding is that singleton pattern can be implemented in one running program. Am I right? If I run my program twice and create a thread in each program, then those threads have to be different threads(different instances,) I think.
                            Given many ideas, I'll keep posting for any update. Thank you so much.

                            @cvp
                            Your solution seems working. Mysterious behavior (which I described) may be caused by my impatient testing. (run a program, close Pythonista, run a program again with no break but only clicking the shortcut icon etc.)

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

                              Again, I'll caveat that I haven't run this, but @TPO's code could be modified to return lock_view from is_active, then you could simply close and relaunch the view. Or, if the view is already active, you could reuse the view, and just replace all the subviews.

                              I'll have to experiment with it.

                              satsuki.kojima 1 Reply Last reply Reply Quote 0
                              • satsuki.kojima
                                satsuki.kojima @JonB last edited by

                                @JonB
                                Ah yes, so you mean the codes of @TPO, posted 2 years ago. Sorry, I did't get it and was misunderstanding your suggestion. Well, I think I need to try it. Will let you know how it works.

                                1 Reply Last reply Reply Quote 0
                                • satsuki.kojima
                                  satsuki.kojima @Guest last edited by

                                  This post is deleted!
                                  cvp 1 Reply Last reply Reply Quote 0
                                  • cvp
                                    cvp @satsuki.kojima last edited by

                                    @satsuki.kojima don't worry, this user has posted in the last 10 topics, everybody saw it.

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

                                      Thank you for your help. vidmate download instasave

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