Python countdown function with input parameter
Question:
I have the following code to show a countdown in a given label in my GUI.
The code looks like this:
def countdown(self, a, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
a.configure(text="time's up!")
else:
a.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)
"a" is at the moment a placeholder variable for my label I will input in this function. My problem is that I don’t know how to remain "a" while reusing my function with:
self.after(1000, self.countdown)
Thanks in advance!
Answers:
As per tkinter doc, signature of after is:
after(delay_ms, callback=None, *args)
So you can pass “a” or “remaining” as arguments and collect it in the positional argument container *args and use them inside your function. By this way, your function remains generic.
I have the following code to show a countdown in a given label in my GUI.
The code looks like this:
def countdown(self, a, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
a.configure(text="time's up!")
else:
a.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)
"a" is at the moment a placeholder variable for my label I will input in this function. My problem is that I don’t know how to remain "a" while reusing my function with:
self.after(1000, self.countdown)
Thanks in advance!
As per tkinter doc, signature of after is:
after(delay_ms, callback=None, *args)
So you can pass “a” or “remaining” as arguments and collect it in the positional argument container *args and use them inside your function. By this way, your function remains generic.