Python Forum
Clicker count increase by 1 each time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Clicker count increase by 1 each time
#1
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()
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,342 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,324 Jan-21-2022, 06:24 PM
Last Post: mcva
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,547 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  Using SoX in Python to Increase mp3 Bitrate DRT 1 1,767 Jul-10-2021, 08:41 PM
Last Post: DRT
  increase and decrease a slice value? KEYS 2 2,107 Nov-10-2020, 11:35 PM
Last Post: KEYS
  Increase Numbers forever and need reset? ATARI_LIVE 4 2,358 Oct-23-2020, 01:55 PM
Last Post: ATARI_LIVE
  The count variable is giving me a hard time in this code D4isyy 2 1,983 Aug-09-2020, 10:32 PM
Last Post: bowlofred
  Please help me with keyboard clicker jurij7 0 1,322 May-04-2020, 07:30 PM
Last Post: jurij7
  How do I add an element to a dic and increase an existing value Kanashi 2 1,865 Nov-21-2019, 02:56 PM
Last Post: ThomasL
  Count to movement according to the time pressed button noartist 1 2,523 Feb-27-2019, 01:33 PM
Last Post: noartist

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020