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.


    How to change the button image?

    Pythonista
    button image
    5
    13
    870
    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.
    • cvp
      cvp @ZinoSama 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')	
      
      1 Reply Last reply Reply Quote 2
      • ccc
        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()
        
        1 Reply Last reply Reply Quote 3
        • ZinoSama
          ZinoSama last edited by

          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)
          
          1 Reply Last reply Reply Quote 0
          • ccc
            ccc last edited by

            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 1 Reply Last reply Reply Quote 1
            • ZinoSama
              ZinoSama @ccc last edited by

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

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @ZinoSama last edited by 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 1 Reply Last reply Reply Quote 0
                • ZinoSama
                  ZinoSama @cvp last edited by

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

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

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

                    1 Reply Last reply Reply Quote 0
                    • Trüff
                      Trüff @cvp 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.

                      cvp 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @Trüff last edited by cvp

                        @Trüff please this old topic

                        Or try this example

                        Pythonista crashes but rerun script after restart of Pythonista

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors