Cookie Clicker using Python Tkinter

michael_linson_0078

Michael Linson

Posted on November 7, 2024

Cookie Clicker using Python Tkinter

Import

from tkinter import *
Enter fullscreen mode Exit fullscreen mode

This imports all functions and classes from the tkinter module, allowing us to create a GUI in Python.

Making Window

 window = Tk()
Enter fullscreen mode Exit fullscreen mode

Initializes the main window for the application. Tk() creates a root window where all widgets (like buttons, labels, etc.) will be placed.

Setting the Size of the Screen

 window.geometry("300x300")
Enter fullscreen mode Exit fullscreen mode

Sets the window dimensions to 300x300 pixels, defining the initial size of the
application window.

Setting the title

 window.title("Cookie Clicker")
Enter fullscreen mode Exit fullscreen mode

Sets the title of the window to "Cookie Clicker," which appears in the title bar.

Background color

window.configure(bg='#b79488')
Enter fullscreen mode Exit fullscreen mode

Sets the background color of the window to #b79488, a soft brown shade. You can customize the color code as desired.

Clicks tracking

number = 0
Enter fullscreen mode Exit fullscreen mode

Initializes a variable number to 0, which will count the number of times the cookie (button) is clicked.

The method that runs when the button is clicked

def ClickBtn():
 global number
 number += 1
 ShowInfo["text"] = "You Clicked " + str(number) + " times."
Enter fullscreen mode Exit fullscreen mode

Function Definition

ClickBtn() is a function that gets called each time the button is clicked.

Global Keyword

global number allows the function to access and modify the number variable defined outside the function increment each time the function is called, it increments the number by 1.

LabelUpdate

Updates the ShowInfo label text to display the current click count. The text changes to "You Clicked X times," where X is the current value of number.

Initializing the Image

ClickingImage = PhotoImage(file="cookie.png")
Enter fullscreen mode Exit fullscreen mode

ImageLoading

Loads an image from a file named "cookie.png" and assigns it to ClickingImage. This image will be used as the icon on the button.

  • Note: Make sure cookie.png is in the same directory as the code file or provide the full path. This image should ideally be a PNG for compatibility with PhotoImage.

Creating the button

 ClickingButton = Button(window, image=ClickingImage, bg="#fab25f",
 fg="blue", font=("Arial", 22), command=ClickBtn)
Enter fullscreen mode Exit fullscreen mode

Button Creation

Creates a button widget (ClickingButton) and assigns it to the main window.

Parameters

  • image=ClickingImage: Sets the button image to the loaded cookie image(ClickingImage).
  • bg="#fab25f": Sets the button background color to a light orange shade.
  • fg="blue": Sets the text color to blue (though this may not show on animage-based button).
    • font=("Arial", 22): Sets the font type to Arial, size 22.
    • command=ClickBtn: Associates the button with the ClickBtn function, so it’s executed when the button is clicked.

Text Label

ShowInfo = Label(window, text="message", font=("Arial", 20),fg="purple")
Enter fullscreen mode Exit fullscreen mode

Explanation:

LabelCreation

Creates a label widget (ShowInfo) to display messages.

Parameters

  • text="message": Sets the initial text to "message" (you can adjust this to a more informative starting message).
  • font=("Arial", 20): Sets the font type to Arial, size 20.
  • fg="purple": Sets the text color to purple.

Adding them to the screen

 ClickingButton.pack()
 ShowInfo.pack()
Enter fullscreen mode Exit fullscreen mode

WidgetPacking

Adds (packs) the button and label widgets to the window.pack() automatically places each widget below the previous one in the center of the window.

Order

The button is packed first, so it appears above the label in the window layout.

The mainloop

window.mainloop()
Enter fullscreen mode Exit fullscreen mode

EventLoop

Starts Tkinter's main event loop, which keeps the window open and responsive until it is closed. The application will wait for user interactions, such as button clicks, while running this loop.

Full code


from tkinter import *
#says to import everything from tkinter

window=Tk()

window.geometry("300x300")

window.title("Cookie Clicker")

window.configure(bg='#b79488')
number=0


def ClickBtn():
        global number
        #shorcut to increase by 1
        number+=1
        #saying to change the text inside of the ShowInfo label
        ShowInfo["text"] = "You Clicked " + str(number) + " times."

#command tells it to execute which code when it is clicked

ClickingImage=PhotoImage(file="cookie.png")

ClickingButton = Button(window,image=ClickingImage, bg="#fab25f", fg="blue",font=("Arial, 22"), command=ClickBtn)
ShowInfo = Label(window, text="message", font=("Arial, 20"), fg="purple")
ClickingButton.pack()
ShowInfo.pack()
window.mainloop()

Enter fullscreen mode Exit fullscreen mode

Output

Image description

Good job guys you have made a simple cookie clicker using tkinter :)

💖 💪 🙅 🚩
michael_linson_0078
Michael Linson

Posted on November 7, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related