How to change the button image?
How do I change the button image from 'play' to 'pause'?
Something like press button once, the image change to 'pause', then press it again, the image change back to 'play'.
What I did is
sender. Image = ui.Image.named('pause_32')
This will make the button blank, totally white
ccc last edited by
sender.image# No space and lowercase
@ZinoSama if, as I think, you want to use a standard Pythonista button, don't type the name displayed at bottom of button, but tap it, it will generate the full needed name
cvp last edited by
@ZinoSama and for your 2nd question, a quick and dirty sample
import ui mv = ui.View() mv.frame = (0,0,300,300) mv.background_color = 'lightgray' b = ui.ButtonItem() idx = 0 b.image = ui.Image.named('iob:pause_32') def b_action(sender): global idx idx = 1 - idx sender.image = ui.Image.named(['iob:pause_32','iob:play_32'][idx]) b.action = b_action mv.right_button_items = (b,) mv.present('sheet')
ccc last edited by ccc
Cool! A nice opportunity for itertools.cycle()...
from itertools import cycle import ui button_image = cycle(ui.Image.named(name) for name in ('iob:pause_32','iob:play_32')) def button_action(sender): sender.image = next(button_image) ui.Button(action=button_action, bg_color='lightgray', image=next(button_image)).present()
Thank you, guys, just by adding 'iob:' makes it working.
Now I get another problem, I have a button action link to this function, and every time I press this button, the whole Pythonista will stuck.
state = False def start(sender): def ani_pause(): sender.image = ui.Image('iob:pause_32') def ani_play(): sender.image = ui.Image('iob:play_32') global state if state is False: ui.animate(ani_pause) state = True start_time = round(time.time()) label = sender.superview['label1'] label2 = sender.superview['label2'] while int(label.text) != '131': time_now = round(time.time()) duration = time_now - start_time time_left = divmod(duration, 360) label2.text = str(time_left) if time_left == 0: label.text = str(int(label.text) + 1) else: continue ui.animate(ani_play) state = False else: state = False ui.animate(ani_play)
ccc last edited by
stateis a weak name because it does not make clear what it is the state of.
is_playingwould be a more self-documenting name.
@ccc you are right, I already changed it. It is still stuck though
It is still stuck though
I guess that your while loop does not stop...
you compare an int with a string ???? It will never be equal thus infinite loop
while int(t) != '131':
@cvp oops, didn’t notice that. Let me try it.
JonB last edited by
You can't put this sort of loop inside a button action, unless the action has in_background.
Trüff last edited by
@cvp Can you tell me how you opened the menu for the Pythonista buttons? Does it have something to do with the hammer and wrench icon on the top right? If yes, I don’t have that icon. It just shows me the 3 other button next to it.