Python Forum

Full Version: Clicker count increase by 1 each time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Trying out making a clicker game and wondering what the likely simple fix to this is. I'm not quite sure how to approach it but I want to have the clicker count increase by 1 each time. Thanks in advance

def button():
    a = 0
    b = 1
    result = a + b
    counter_label = Label(root, text=str(result) + " cookies")
    counter_label.grid(row=4, column=2)


button_click = Button(root, command=button, text="Click", width=15, height=5)
button_click.grid(row=2, column=2)

root.mainloop()
result is a variable that only exists within the context of button(). Once the function is done the variable goes away. If functions remembered local variables it would greatly limit their usefulness.

There are many ways to fix this problem. You could use a global variable for the result.
import tkinter as tk

cookies = 0 # global variable

def button():
    global cookies  # Tells Python to use global cookies instead of making a local variable
    cookies += 1
    cookie_label['text'] = f'{cookies} cookies'

root = tk.Tk()
cookie_label = tk.Label(root, text='zero cookies')
cookie_label.grid(row=4, column=2)
tk.Button(root, command=button, text="Click", width=15, height=5) \
             .grid(row=2, column=2)
 
root.mainloop()
You could use controls that not only display, but contain information. Here I use an IntVar to keep the cookie count. This is kind of like using a global variable, but the variable is mutable so I don't have to use the "global" keyword. I also only have to keep track of one thing, the "cookies" IntVar. In the first example I had to remember the "cookies" global variable and the "cookie_label".
import tkinter as tk

def button():
    cookies.set(cookies.get() + 1)

root = tk.Tk()
cookies = tk.IntVar()  # Special object that knows how to talk to tkinter widgets
tk.Label(root, textvariable=cookies).grid(row=1, column=0)
tk.Label(root, text='cookies').grid(row=1, column=1)
tk.Button(root, command=button, text="Click", width=15, height=5) \
             .grid(row=0, column=0, columnspan=2)
 
root.mainloop()
You could write a class. A class has it's own namespace where the variables and functions are called "attributes". As you can see it looks very much like the "global" example except now we use "self.cookies" instead of "global cookies". The advantages of classes will become apparent as you start writing more complex code.
import tkinter as tk

class Cookies(): # A collection of variables and functions

    def __init__(self, parent): # Gets called when you create "instance" of class
        self.cookies = 0
        self.label = tk.Label(parent, text='zero cookies')
        self.label.grid(row=1, column=1)
        tk.Button(parent, command=self.button, text="Click", width=15, height=5) \
             .grid(row=0, column=0, columnspan=2)

    def button(self):  # A "method" is a function owned by a class
        self.cookies += 1
        self.label['text'] = f'{self.cookies} cookies'
 
root = tk.Tk()
Cookies(root)
root.mainloop()