How can I decrease the number on a button?
<p>I'm trying to create a GUI that has only one button. Each time the button is pressed the number on the button should decrease. So I wrote this:</p>

<pre><code>import Tkinter

def countdown(x):        
   x -= 1

top = Tkinter.Tk()

def helloCallBack():    
    countdown(x)    

B = Tkinter.Button(top, text=x, command=helloCallBack )   
B.pack()       
top.mainloop()
</code></pre>

<p>I want that button's text to be like 5 4 3 2 1, so where should I put the <code>x=5</code>?</p>

<p>You can create global variable and use it in function <code>countdown</code> (but you have to use <code>global</code>) and then you can use <code>config(text=...)</code> to change text on button.</p>

<pre><code>import Tkinter as tk

# --- functions ---

def countdown():
    global x # use global variable

    x -= 1

    B.config(text=x)

# --- main ---

# create global variable (can be in any place - before/after function, before/after Tk())
x = 5

top = tk.Tk()

B = tk.Button(top, text=x, command=countdown)   
B.pack()

top.mainloop()    
</code></pre>

<p>Or you can use <code>IntVar()</code> and <code>textvariable</code> - so you don't need <code>config(text=...)</code> and <code>global</code>. </p>

<pre><code>import Tkinter as tk

# --- functions ---

def countdown():
    x.set( x.get()-1 )

# --- main ---

top = tk.Tk()

# create global variable (have to be after Tk())
x = tk.IntVar()
x.set(5)

# use `textvariable` and `lambda` to run `countdown` with `x`
B = tk.Button(top, textvariable=x, command=countdown)   
B.pack()

top.mainloop()
</code></pre>

<p>I use <code>lambda</code> to run <code>countdown()</code> with <code>x</code> so I can use <code>countdown</code> with many buttons with different <code>IntVar</code>. (you can't do the same in first example when you use <code>int</code>)</p>

<pre><code>import Tkinter as tk

# --- functions ---

def countdown(var):
    var.set( var.get()-1 )

# --- main ---

top = tk.Tk()

# create global variable (have to be after Tk())
x = tk.IntVar()
x.set(5)

y = tk.IntVar()
y.set(5)

# use `textvariable` and `lambda` to run `countdown` with `x`
B1 = tk.Button(top, textvariable=x, command=lambda:countdown(x))   
B1.pack()

B2 = tk.Button(top, textvariable=y, command=lambda:countdown(y))   
B2.pack()

top.mainloop()
</code></pre>

<hr>

<p>BTW: you can use <code>after(miliseconds, functio_name)</code> to run function <code>countdown</code> every 1s and countdown without clicking :)</p>

<pre><code>import Tkinter as tk

# --- functions ---

def countdown(var):
    var.set( var.get()-1 )

    # run again time after 1000ms (1s)
    top.after(1000, lambda:countdown(var))

# --- main ---

top = tk.Tk()

# create global variable (have to be after Tk())
x = tk.IntVar()
x.set(5)

# use `textvariable` and `lambda` to run `countdown` with `x`
B = tk.Button(top, textvariable=x)   
B.pack()

# run first time after 1000ms (1s)
top.after(1000, lambda:countdown(x))

top.mainloop()
</code></pre>

python python-2.7 tkinter