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.


    Play and pause

    Pythonista
    ui module ui.button ui.view
    3
    5
    3423
    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.
    • yas
      yas last edited by

      The pause button is not working

      import ui
      
      class view():
      	def __init__(self):
      		self.flag = True
      		content = ui.TableViewCell()
      		play = ui.Button(frame = (259,0,50,content.height))
      		pause = ui.Button(frame = play.frame)
      		
      		play.image = ui.Image('iob:ios7_play_256')
      		pause.image = ui.Image('iob:pause_256')
      		pause.hidden = True
      		play.action = self.Play
      		pause.action = self.Pause
      		label = ui.Label(frame =(0,0,0,content.height))
      		label.touch_enabled = False
      		
      		label.background_color = 'blue'
      		
      
      		self.play = play
      		self.pause = pause
      		self.label = label
      		self.flag = False
      		content.content_view.add_subview(play)
      		content.content_view.add_subview(pause)
      		content.content_view.add_subview(label)
      		self.content = content
      	def table(self):
      		return self.content
      	
      	@ui.in_background
      	def Play(self,sender):
      		sender.hidden = True
      		self.pause.hidden = False
      		import time
      		for a in range(320):
      			self.label.width = a
      			time.sleep(0.1)
      			if self.flag:
      				self.flag = False
      				return 
      	
      	@ui.in_background
      	def Pause(self,sender):
      		sender.hidden = True
      		self.play.hidden = False
      		self.flag = True
      		
      class Llist():
      	def tableview_number_of_rows(self,t,s):
      		return 40
      	def tableview_cell_for_row(self,t,s,r):
      		return view().table()
      		
      a=ui.TableView()
      a.data_source = Llist()
      a.allows_selection = False
      a.present()
      
      mikael 1 Reply Last reply Reply Quote 0
      • mikael
        mikael @yas last edited by

        @yas, try using ui.delay instead of time.sleep.

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

          the confusing thing about @ui.in_background is that it is not actually on its own background thread -- it is on a shared thread that runs the main script, and any other in_background calls.

          Since both play and pause are in_background'd, pause does not run until play is done.

          In your case, pause does not need to be in_background, since it is just setting a flag and returns immediately.

          An alternative would be to either use a ui.delay inside zplay, or implement a custom decorator that kicks off a Thread. You could also use a custom view, and the update method to do some action repeatedly.

          Here is a thread that covera many of these topics.
          https://forum.omz-software.com/topic/2285/timer-with-ui/8

          1 Reply Last reply Reply Quote 1
          • mikael
            mikael last edited by

            ... or, once again, use Scripter to make these kinds of things really easy, with specific support for pausing animation.

            1 Reply Last reply Reply Quote 1
            • yas
              yas last edited by

              Thanks everyone,
              @JonB You cleared my doubt on @ui.in_background, Now I'm using threading instead of ui.in_background

              import ui,threading as thr
              
              class view():
              	def __init__(self):
              		self.flag = True
              		content = ui.TableViewCell()
              		play = ui.Button(frame = (259,0,50,content.height))
              		pause = ui.Button(frame = play.frame)
              		
              		play.image = ui.Image('iob:ios7_play_256')
              		pause.image = ui.Image('iob:pause_256')
              		pause.hidden = True
              		play.action = self.Play
              		pause.action = self.Pause
              		label = ui.Label(frame =(0,0,0,content.height))
              		label.touch_enabled = False
              		
              		label.background_color = 'blue'
              		
              
              		self.play = play
              		self.pause = pause
              		self.label = label
              		self.flag = False
              		content.content_view.add_subview(play)
              		content.content_view.add_subview(pause)
              		content.content_view.add_subview(label)
              		self.content = content
              	def table(self):
              		return self.content
              		
              	def Play(self,sender):
              		t = thr.Thread(target=self.Playy,args=(sender,))
              		t.daemon = True
              		t.start()
              	def Pause(self,sender):
              		t = thr.Thread(target=self.Pausee,args=(sender,))
              		t.daemon = True
              		t.start()
              	def Playy(self,sender):
              		sender.hidden = True
              		self.pause.hidden = False
              		import time
              		for a in range(320):
              			self.label.width = a
              			time.sleep(0.1)
              			if self.flag:
              				self.flag = False
              				return 
              	
              
              	def Pausee(self,sender):
              		sender.hidden = True
              		self.play.hidden = False
              		self.flag = True
              		
              class Llist():
              	def tableview_number_of_rows(self,t,s):
              		return 40
              	def tableview_cell_for_row(self,t,s,r):
              		return view().table()
              		
              a=ui.TableView()
              a.data_source = Llist()
              a.allows_selection = False
              a.present()
              
              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors