Import
from tkinter import *
This imports all functions and classes from the tkinter module, allowing us to create a GUI in Python.
Making Window
window = Tk()
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")
Sets the window dimensions to 300x300 pixels, defining the initial size of the
application window.
Setting the title
window.title("Cookie Clicker")
Sets the title of the window to "Cookie Clicker," which appears in the title bar.
Background color
window.configure(bg='#b79488')
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
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."
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")
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 withPhotoImage
.
Creating the button
ClickingButton = Button(window, image=ClickingImage, bg="#fab25f",
fg="blue", font=("Arial", 22), command=ClickBtn)
Button Creation
Creates a button widget (ClickingButton)
and assigns it to the main window.
Parameters
-
image=ClickingImage
: Sets the button image to the loaded cookieimage(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")
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()
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()
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()
Output
Good job guys you have made a simple cookie clicker using tkinter :)
Top comments (0)