0

Program was working fine until I added the Start button on the bottom. Program seems to be running properly except that the timer doesn't update. I would like the counter to start counting down as soon as the "Start" button is clicked in my program.

My code:

import time
import tkinter as tk

def DisplayCountdown():
    # Time
    m = 0
    s = 3

    print('Running DisplayCountdown function')
    if m == 0 and s == 0:
        count_down_display.configure(text='Stand up!')
    elif s == 0:
        s=59
        m-=1
        count_down_display.after(1000, DisplayCountdown)
    else:
        s-=1
        countdown_display.configure(text='%s minutes %s seconds' % (m,s))
        countdown_display.after(1000, DisplayCountdown)

# Window
window = tk.Tk()
window.title('Stand Up Timer')
window.configure(bg = 'black')

# Information label
start_label = tk.Label(window,
                       font = 'ariel 40',
                       bg = 'black',
                       fg = 'red',
                       text = 'Click the start button to begin the timer.')
start_label.grid(row = 0, column = 0)

# Display Countdown
countdown_display = tk.Label(window,
                            font = 'ariel 40',
                            bg = 'black',
                            fg = 'red',
                            text = 'Countdown displayed here!')
countdown_display.grid(row = 1, column = 0)

# Start button
start_button = tk.Button(window,
                         text='Start',
                         command=DisplayCountdown)
start_button.grid(row = 2, column = 0)

# Window main loop
window.mainloop()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • `m` and `s` will be 0 and 3 ever call including in after statement. This will results in always performing the `else` portion of your `if/elif/else` statement. Also please write your widgets all on one line. It is easier to read all the code in a more condensed fashion. Also you have `count_down_display` and `countdown_display` So there is a another problem. – Mike - SMT Feb 07 '20 at 20:21
  • Thank you so much for you help. I'm pretty new to stack overflow not sure what you mean by put the widget all in one line. – HelloWorld42 Feb 07 '20 at 20:27

1 Answers1

1

You have a few things to correct here.

First these 2 lines:

m = 0
s = 3

They cannot be in the function like this. Basically every loop will always start off a m=0 s=3. So instead pass them into the function as arguments.

Next Update your command and after statements to use a lambda to pass the initial and new time variables.

Next get rid of all those comments. They are redundant. Good code does not need comments as it is very obvious what is going on. Comments are normally need for complex sections of code that may need explaining for your self or other down the road.

I don't like going to new lines after every argument for widgets. Its just messy IMO the way you have it now. I write everything on a single line unless we overreach PEP8 recommended line length then find a good spot to go to new line.

Working example:

import tkinter as tk


def display_countdown(m, s):
    print('Running DisplayCountdown function')
    if m == 0 and s == 0:
        countdown_display.configure(text='Stand up!')
    elif s == 0:
        s = 59
        m -= 1
        countdown_display.after(1000, lambda: display_countdown(m, s))
    else:
        s -= 1
        countdown_display.configure(text='%s minutes %s seconds' % (m, s))
        countdown_display.after(1000, lambda: display_countdown(m, s))


window = tk.Tk()
window.title('Stand Up Timer')
window.configure(bg='black')

start_label = tk.Label(window, font='ariel 40', bg='black', fg='red',
                       text='Click the start button to begin the timer.')
countdown_display = tk.Label(window, font='ariel 40', bg='black', fg='red',
                             text='Countdown displayed here!')
start_button = tk.Button(window, text='Start', 
                         command=lambda: display_countdown(0, 3))
start_label.grid(row=0, column=0)
countdown_display.grid(row=1, column=0)
start_button.grid(row=2, column=0)
window.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • ***"write the widgets all on one line"***: That violates PEP8 length! – stovfl Feb 07 '20 at 20:36
  • @stovfl Well true enough but in that case we can drop down one line. Not a new line for each argument. I wonder why my PyCharm sets the max line length to 120. – Mike - SMT Feb 07 '20 at 20:44
  • @stovfl look at that there is already a Q/A about PyCharm max length. [Why does PyCharm use 120 Character Lines even though PEP8 Specifies 79?](https://stackoverflow.com/questions/26808681/why-does-pycharm-use-120-character-lines-even-though-pep8-specifies-79)... Oh God does that mean I have to go back and correct the last 8000 lines of code I have worked on... NOOOO :D – Mike - SMT Feb 07 '20 at 20:54
  • @Mike-SMT use [black](https://black.readthedocs.io/en/stable/) for a consistent coding style. It will adjust your code for you. – Bryan Oakley Feb 07 '20 at 21:36
  • @BryanOakley I have always tried to follow PEP8 from memory but I will take a look at your link. It may be nice not to have to think about it so much. – Mike - SMT Feb 07 '20 at 21:45