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
-
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()
-
@yas, try using ui.delay instead of time.sleep.
-
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 -
... or, once again, use Scripter to make these kinds of things really easy, with specific support for pausing animation.
-
Thanks everyone,
@JonB You cleared my doubt on @ui.in_background, Now I'm using threading instead of ui.in_backgroundimport 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()