Forum Archive

How to change the button image?

ZinoSama

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

sender.image # No space and lowercase i

https://omz-software.com/pythonista/docs/ios/ui.html#button

cvp

@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

ui.Image.named('iob:pause_32')

cvp

@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

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()
ZinoSama

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)[1]
            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

state is a weak name because it does not make clear what it is the state of.

is_playing would be a more self-documenting name.

ZinoSama

@ccc you are right, I already changed it. It is still stuck though

cvp

@ZinoSama said

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':
ZinoSama

@cvp oops, didn’t notice that. Let me try it.

JonB

You can't put this sort of loop inside a button action, unless the action has in_background.

Trüff

@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.

cvp

@Trüff please this old topic

Or try this example

Pythonista crashes but rerun script after restart of Pythonista