Making a countdown timer with Python and Tkinter?












6















I want to set a label in Tkinter using my countdown timer function. Right now all it does is set the lable to "10" once 10 is reached and I don't really understand why. Also, even if I have the timer print to a terminal instead the "Time's up!" bit never prints.



import time
import tkinter as tk

class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text="null")
self.label.pack()
self.countdown()
self.root.mainloop()

# Define a timer.
def countdown(self):
p = 10.00
t = time.time()
n = 0
# Loop while the number of seconds is less than the integer defined in "p"
while n - t < p:
n = time.time()
if n == t + p:
self.label.configure(text="Time's up!")
else:
self.label.configure(text=round(n - t))

app=App()









share|improve this question

























  • here's a code example of a countdown implemented using Tkinter

    – jfs
    Mar 3 '15 at 9:15
















6















I want to set a label in Tkinter using my countdown timer function. Right now all it does is set the lable to "10" once 10 is reached and I don't really understand why. Also, even if I have the timer print to a terminal instead the "Time's up!" bit never prints.



import time
import tkinter as tk

class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text="null")
self.label.pack()
self.countdown()
self.root.mainloop()

# Define a timer.
def countdown(self):
p = 10.00
t = time.time()
n = 0
# Loop while the number of seconds is less than the integer defined in "p"
while n - t < p:
n = time.time()
if n == t + p:
self.label.configure(text="Time's up!")
else:
self.label.configure(text=round(n - t))

app=App()









share|improve this question

























  • here's a code example of a countdown implemented using Tkinter

    – jfs
    Mar 3 '15 at 9:15














6












6








6


3






I want to set a label in Tkinter using my countdown timer function. Right now all it does is set the lable to "10" once 10 is reached and I don't really understand why. Also, even if I have the timer print to a terminal instead the "Time's up!" bit never prints.



import time
import tkinter as tk

class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text="null")
self.label.pack()
self.countdown()
self.root.mainloop()

# Define a timer.
def countdown(self):
p = 10.00
t = time.time()
n = 0
# Loop while the number of seconds is less than the integer defined in "p"
while n - t < p:
n = time.time()
if n == t + p:
self.label.configure(text="Time's up!")
else:
self.label.configure(text=round(n - t))

app=App()









share|improve this question
















I want to set a label in Tkinter using my countdown timer function. Right now all it does is set the lable to "10" once 10 is reached and I don't really understand why. Also, even if I have the timer print to a terminal instead the "Time's up!" bit never prints.



import time
import tkinter as tk

class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text="null")
self.label.pack()
self.countdown()
self.root.mainloop()

# Define a timer.
def countdown(self):
p = 10.00
t = time.time()
n = 0
# Loop while the number of seconds is less than the integer defined in "p"
while n - t < p:
n = time.time()
if n == t + p:
self.label.configure(text="Time's up!")
else:
self.label.configure(text=round(n - t))

app=App()






python time tkinter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 26 '18 at 15:17









Aran-Fey

20.7k53571




20.7k53571










asked May 15 '12 at 8:36









Ryan HasseRyan Hasse

1201214




1201214













  • here's a code example of a countdown implemented using Tkinter

    – jfs
    Mar 3 '15 at 9:15



















  • here's a code example of a countdown implemented using Tkinter

    – jfs
    Mar 3 '15 at 9:15

















here's a code example of a countdown implemented using Tkinter

– jfs
Mar 3 '15 at 9:15





here's a code example of a countdown implemented using Tkinter

– jfs
Mar 3 '15 at 9:15












1 Answer
1






active

oldest

votes


















8














Tkinter already has an infinite loop running (the event loop), and a way to schedule things to run after a period of time has elapsed (using after). You can take advantage of this by writing a function that calls itself once a second to update the display. You can use a class variable to keep track of the remaining time.



import Tkinter as tk

class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label = tk.Label(self, text="", width=10)
self.label.pack()
self.remaining = 0
self.countdown(10)

def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining

if self.remaining <= 0:
self.label.configure(text="time's up!")
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)

if __name__ == "__main__":
app = ExampleApp()
app.mainloop()





share|improve this answer


























  • That explains a lot, thank you. I was wondering why it only updates once the last item is reached even after I rewrote it a few different ways.

    – Ryan Hasse
    May 15 '12 at 11:49











  • @RyanHasse: the display only updates when Tkinter can respond to events that tell it to update the display. These events only get processed by the event loop. If you have your own loop, it will starve the event loop, preventing redraw requests from happening.

    – Bryan Oakley
    May 15 '12 at 12:54











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10596988%2fmaking-a-countdown-timer-with-python-and-tkinter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









8














Tkinter already has an infinite loop running (the event loop), and a way to schedule things to run after a period of time has elapsed (using after). You can take advantage of this by writing a function that calls itself once a second to update the display. You can use a class variable to keep track of the remaining time.



import Tkinter as tk

class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label = tk.Label(self, text="", width=10)
self.label.pack()
self.remaining = 0
self.countdown(10)

def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining

if self.remaining <= 0:
self.label.configure(text="time's up!")
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)

if __name__ == "__main__":
app = ExampleApp()
app.mainloop()





share|improve this answer


























  • That explains a lot, thank you. I was wondering why it only updates once the last item is reached even after I rewrote it a few different ways.

    – Ryan Hasse
    May 15 '12 at 11:49











  • @RyanHasse: the display only updates when Tkinter can respond to events that tell it to update the display. These events only get processed by the event loop. If you have your own loop, it will starve the event loop, preventing redraw requests from happening.

    – Bryan Oakley
    May 15 '12 at 12:54
















8














Tkinter already has an infinite loop running (the event loop), and a way to schedule things to run after a period of time has elapsed (using after). You can take advantage of this by writing a function that calls itself once a second to update the display. You can use a class variable to keep track of the remaining time.



import Tkinter as tk

class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label = tk.Label(self, text="", width=10)
self.label.pack()
self.remaining = 0
self.countdown(10)

def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining

if self.remaining <= 0:
self.label.configure(text="time's up!")
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)

if __name__ == "__main__":
app = ExampleApp()
app.mainloop()





share|improve this answer


























  • That explains a lot, thank you. I was wondering why it only updates once the last item is reached even after I rewrote it a few different ways.

    – Ryan Hasse
    May 15 '12 at 11:49











  • @RyanHasse: the display only updates when Tkinter can respond to events that tell it to update the display. These events only get processed by the event loop. If you have your own loop, it will starve the event loop, preventing redraw requests from happening.

    – Bryan Oakley
    May 15 '12 at 12:54














8












8








8







Tkinter already has an infinite loop running (the event loop), and a way to schedule things to run after a period of time has elapsed (using after). You can take advantage of this by writing a function that calls itself once a second to update the display. You can use a class variable to keep track of the remaining time.



import Tkinter as tk

class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label = tk.Label(self, text="", width=10)
self.label.pack()
self.remaining = 0
self.countdown(10)

def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining

if self.remaining <= 0:
self.label.configure(text="time's up!")
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)

if __name__ == "__main__":
app = ExampleApp()
app.mainloop()





share|improve this answer















Tkinter already has an infinite loop running (the event loop), and a way to schedule things to run after a period of time has elapsed (using after). You can take advantage of this by writing a function that calls itself once a second to update the display. You can use a class variable to keep track of the remaining time.



import Tkinter as tk

class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label = tk.Label(self, text="", width=10)
self.label.pack()
self.remaining = 0
self.countdown(10)

def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining

if self.remaining <= 0:
self.label.configure(text="time's up!")
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)

if __name__ == "__main__":
app = ExampleApp()
app.mainloop()






share|improve this answer














share|improve this answer



share|improve this answer








edited May 15 '12 at 11:12

























answered May 15 '12 at 11:07









Bryan OakleyBryan Oakley

219k22266430




219k22266430













  • That explains a lot, thank you. I was wondering why it only updates once the last item is reached even after I rewrote it a few different ways.

    – Ryan Hasse
    May 15 '12 at 11:49











  • @RyanHasse: the display only updates when Tkinter can respond to events that tell it to update the display. These events only get processed by the event loop. If you have your own loop, it will starve the event loop, preventing redraw requests from happening.

    – Bryan Oakley
    May 15 '12 at 12:54



















  • That explains a lot, thank you. I was wondering why it only updates once the last item is reached even after I rewrote it a few different ways.

    – Ryan Hasse
    May 15 '12 at 11:49











  • @RyanHasse: the display only updates when Tkinter can respond to events that tell it to update the display. These events only get processed by the event loop. If you have your own loop, it will starve the event loop, preventing redraw requests from happening.

    – Bryan Oakley
    May 15 '12 at 12:54

















That explains a lot, thank you. I was wondering why it only updates once the last item is reached even after I rewrote it a few different ways.

– Ryan Hasse
May 15 '12 at 11:49





That explains a lot, thank you. I was wondering why it only updates once the last item is reached even after I rewrote it a few different ways.

– Ryan Hasse
May 15 '12 at 11:49













@RyanHasse: the display only updates when Tkinter can respond to events that tell it to update the display. These events only get processed by the event loop. If you have your own loop, it will starve the event loop, preventing redraw requests from happening.

– Bryan Oakley
May 15 '12 at 12:54





@RyanHasse: the display only updates when Tkinter can respond to events that tell it to update the display. These events only get processed by the event loop. If you have your own loop, it will starve the event loop, preventing redraw requests from happening.

– Bryan Oakley
May 15 '12 at 12:54




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10596988%2fmaking-a-countdown-timer-with-python-and-tkinter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas