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.
Refresh ui
-
I know there are better ways to do this, I am just wanting to do this as an example.
I want to take an image view and rotate through several images in a loop.
I found that the command sets_needs_display(), but it does not seem to refresh the imagesimport ui import time # first get a reference to the image you saved in your Python folder walking_man_image = ui.Image.named('./assests/sprites/image1.BMP') walking_man_imageview = ui.ImageView(frame=(30, 0, 180, 180)) walking_man_imageview.image = walking_man_image def start_button_touch_up_inside(sender): image_counter = 1 while image_counter <= 10: if image_counter == 1: walking_man_image = ui.Image.named('./assests/sprites/image1.BMP') walking_man_imageview.image = walking_man_image elif image_counter == 2: walking_man_image = ui.Image.named('./assests/sprites/image2.BMP') walking_man_imageview.image = walking_man_image elif image_counter == 3: walking_man_image = ui.Image.named('./assests/sprites/image3.BMP') walking_man_imageview.image = walking_man_image elif image_counter == 4: walking_man_image = ui.Image.named('./assests/sprites/image4.BMP') walking_man_imageview.image = walking_man_image # now wait for a fraction of second time.sleep(0.2) image_counter = image_counter + 1 #print(image_counter) view.set_needs_display() view = ui.load_view() # lastly, add the Imageview into the existing view view.add_subview(walking_man_imageview) view.present('sheet')
-
@mrcoxall: does the last image display once the loop is finished? I just tested it (using different images) and it seemed that the only issue was that the loop was blocking the UI thread, so it couldn't update and render the new information. To fix this just add
@ui.in_background
abovedef start_button_touch_up_inside(sender)
and it should work fine. -
@Cethric said:
@ui.in_background
Works great.
Thanks so much -
import time, ui imageview = ui.ImageView(frame=(30, 0, 180, 180)) # a list of ten images images = [ui.Image.named('plf:Hud{}'.format(i)) for i in range(10)] def start_button_touch_up_inside(sender): for i in range(30): imageview.image = images[i % len(images)] # now wait for a fraction of second time.sleep(0.2) # view.set_needs_display() #view = ui.load_view() view = ui.View() # lastly, add the Imageview into the existing view view.add_subview(imageview) view.present() start_button_touch_up_inside(None)
-
@mrcoxall , here is another way. Not really smart. Just an example. But you mentioned set_needs_display method. That method is used in a custom ui class when you define the draw method. The draw method will be called automatically when the frame or bounds change, but you can force a redraw event by calling set_needs_display, which in turn will call the draw method if it has been defined. Also, I am using ui.Delay here. Both ways (ui.in_background/ui.Delay) are valid depending on what you need to do. This is a simple case. But even doing a thread in Python is not that difficult on easy stuff. It can get a lot more complicated if you are using with the web etc. but anyway, I hope it helps just to see from another way.
# Pythonista Forum - @Phuket2 import ui, editor # get the numbered spades and Hearts cards (images) img_list = ['card:Spades' + str(i) for i in range(2, 11)] + ['card:Hearts' + str(i) for i in range(2, 11)] class MyClass(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.img_rect = (0, 0, 200, 300) self.img_num = 0 self.make_view() def make_view(self): btn = ui.Button(frame=(0, 0, 32, 100)) btn.title = 'Stop' btn.size_to_fit() btn.center = self.bounds.center() btn.y = self.height - btn.height - 10 btn.action = self.btn_stop self.add_subview(btn) self.start() def start(self): # ui.Delay, continually calls this method, until the stop button # is pressed or the view will close self.set_needs_display() ui.delay(self.start, .8) if self.img_num == len(img_list)-1: self.img_num = 0 else: self.img_num += 1 def draw(self): r = ui.Rect(*self.img_rect) r.center(self.bounds.center()) ui.Image.named(img_list[self.img_num]).draw(*r) def btn_stop(self, sender): self.name = 'delay is stopped' ui.cancel_delays() def will_close(self): ui.cancel_delays() if __name__ == '__main__': _use_theme = True w, h = 600, 800 f = (0, 0, w, h) style = 'sheet' mc = MyClass(frame=f, bg_color='white') if not _use_theme: mc.present(style=style, animated=False) else: editor.present_themed(mc, theme_name='Oceanic', style=style, animated=False)