0

I've created a simple function with a Tkinter module to create a timer. But I face some issues and can't get it work : with an answer.get(), I want to know how long the counter will be working, but sec -= 1 prevents the function to carry on. What am I doing wrong, please ?

Thank you for your help, here's a part of the code :

def timer():
   labelUn.config(text='Enter the required number of seconds:')
   def launchTimer():
       sec=int(answer.get())
       def launch():
           nonglobal sec
           if sec>0:
              sec -= 1
              labelUn.config(text=sec)
              print(sec)
              root.after(1000, launch)
           if sec<=0:
               labelUn.config(text="that's it!")
               print(sec)
               return
      button1.config(command=launch)

   button1.config(command=launchTimer)
Jack LM
  • 1
  • 1
  • Welcome to StackOverflow. Please take the [tour], learn asking good questions stackoverflow.com/help/how-to-ask, make a [mcve]. If you are looking for help with debugging code see https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Yunnosch May 28 '17 at 14:09
  • 2
    The usual mistake... change `command=launch()` to `command=launch` and `command=launchTimer()` to `command=launchTimer`. (At least that's _one_ of your mistakes; there are probably more...) – Aran-Fey May 28 '17 at 14:09
  • BTW, `global sec` won't allow `launch` to modify the `sec` defined in the outer section of `launchTimer`, you need `nonlocal sec` for that. However, `nonlocal` is a Python 3 feature, if you have to do this in Python 2 you'll need another approach. – PM 2Ring May 28 '17 at 14:18
  • Hi, thank you for your help, I've translated it into English in order to post it on Stackoverflow, I made mistakes and I'm sorry for this. I have edited my code. I use Python 3 so it works, and I think `nonlocal` has solved part of the problem : now I've got `unsupported operand type(s) for -=: 'str' and 'int` – Jack LM May 28 '17 at 14:19
  • I've reopened your question because it is no longer a duplicate of [that other question](https://stackoverflow.com/questions/8269096/why-is-button-parameter-command-executed-when-declared). But now you should fix the code so that it's a [mcve] so that we can easily reproduce your problem. Can we assume that you're using Python 3? – PM 2Ring May 28 '17 at 14:24
  • Sounds like sec is a string, not an integer. How do would you subtract those? – OneCricketeer May 28 '17 at 14:24
  • You're right @cricket_007, now that's fixed. But my countdown isn't displayed correctly, I'm working on that. Yes I'm using Python 3. – Jack LM May 28 '17 at 14:38
  • Are you expecting it to count down repeatedly? Your code only decreases the number one time when you click the button – OneCricketeer May 28 '17 at 14:41
  • @cricket_007 I've fixed it, sorry I didn't edit quickly enough. I want it to stop when it reaches 0. – Jack LM May 28 '17 at 14:46
  • Thank you very much for helping me, my program is working now! And sorry for my questions, I've just started programming. – Jack LM May 28 '17 at 14:55

0 Answers0