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.


    Pythonista Countdown on button with .pyui

    Pythonista
    pythonista help needed ui.button ui.datepicker pyui
    5
    10
    4003
    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.
    • JeremyMH
      JeremyMH last edited by ccc

      How do I make a timer start to countdown with the click of a button? Here is an image of what I have so far. Im trying to make it so when you click "Launch", the timer starts. Can anyone help me? Thanks

      mikael 1 Reply Last reply Reply Quote 0
      • mikael
        mikael @JeremyMH last edited by

        @JeremyMH, if you have created this in the UI editor, now you have to move to the code.

        You need to set the button’s action be a function that updates the timer and, if the timer should still be running, calls itself after a second with ui.delay.

        1 Reply Last reply Reply Quote 1
        • JeremyMH
          JeremyMH last edited by

          sorry im kind of a noob. can you please explain further! thanks

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

            like for example, how would i make it call the function and how woukd i change the value of the ui? i havent used python in a while.

            mikael 1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              @JeremyMH Can you please paste here the code that you have so far?

              It is easier to debug Python code than it is to debug English prose.

              1 Reply Last reply Reply Quote 0
              • mikael
                mikael @JeremyMH last edited by

                @JeremyMH, let’s check the basics first.

                • Have you read the basic usage page?
                • Have you checked this section of the ui module manual and tried the example of tying a button to an action?
                1 Reply Last reply Reply Quote 0
                • JeremyMH
                  JeremyMH last edited by

                  import ui
                  
                  def startlaunch(countdown):
                  	viewvalue = countdown.superview
                  	tapped = viewvalue['launchbutton']
                  	started = False
                  	if tapped:
                  		started = True
                  	while tapped: 
                  		ui.delay(countdowntick, 1)
                  	
                  def countdowntick():
                  	print("hi")
                  
                  v = ui.load_view()
                  v.present('sheet')
                  

                  but that just crashes it

                  mikael 1 Reply Last reply Reply Quote 0
                  • mikael
                    mikael @JeremyMH last edited by

                    @JeremyMH, it looks to me that your code should do nothing, not even give an error.

                    Did you try the code in my second link above?

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

                      Look at the stopwatch1.py and stopwatch1.pyui example in the following GitHub repository. May be this is what you are looking for.
                      https://github.com/encela95dus/ios_pythonista_examples

                      1 Reply Last reply Reply Quote 2
                      • BapeHiks
                        BapeHiks last edited by BapeHiks

                        @JeremyMH a foundation vote that proposed @enceladus but without the editor UI

                        import ui
                        class StopWatch(ui.View):
                        	def __init__(self, *args, **kwargs):
                        		super().__init__(*args, **kwargs)
                        		self.value = 0
                        		self.state = 'stop'
                        		self.update_interval = .1
                        
                        	def draw(self):
                        		t0 = (self.value // (600 * 60), self.value // 600, self.value // 10)
                        		t1 = (t0[0], t0[1] % 60, t0[2] % 60)
                        		ui.draw_string(
                        			"{:02}:{:02}:{:02}".format(*t1),
                        			font=('Helvetica', 20),
                        			rect=(150, 0, 0, 0),
                        			color='black',
                        			alignment=ui.ALIGN_CENTER)
                        
                        	def update(self):
                        		if self.state == 'run':
                        			self.value += 1
                        		self.set_needs_display()
                        
                        
                        def button_action(sender):
                        	v1 = sender.superview['Watch']
                        	sender.hidden = True
                        	if sender.title == 'Reset':
                        		v1.value = 0
                        		v1.state = 'stop'
                        		sender.superview['Start'].hidden = False
                        	elif sender.title == 'Start':
                        		v1.value = 0
                        		v1.state = 'run'
                        		sender.superview['Stop'].hidden = False
                        	elif sender.title == 'Stop':
                        		v1.state = 'stop'
                        		sender.superview['Reset'].hidden = False
                        		
                        v = ui.View(width=397, height=271)
                        
                        v.add_subview(StopWatch(name = 'Watch', frame = (0, 30, 345.00, 76.00)))
                        v.present('sheet')
                        
                        for btn in ['Reset', 'Stop', 'Start']:
                        	v.add_subview(ui.Button(frame=(v.center.x-40, v.center.y-40, 80, 80), name=btn, border_width=1, corner_radius=40, action=button_action))
                        	v[btn].title = btn
                        	if btn != 'Start':
                        		v[btn].hidden = True```
                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors