0

I'm making some kind of cookie clicker game with Tkinter to practise my Python skills.

This is my code so far:

""" author: Elxas866 file: BubbleClicker.py descr.: A cookie clicker-like game """ from tkinter import * from PIL import ImageTk, Image clicks = 0 def counter(): global clicks clicks += 1 print(clicks) def main(): root = Tk() root.minsize(500, 500) #pixels root.title("Bubble Cliker") bubble = Image.open("assets/Bubble.png") Bubble = ImageTk.PhotoImage(bubble) image = Button(image=Bubble, command=counter) image.pack() image.place(relx=0.5, rely=0.5, anchor=CENTER) score = Label(text=clicks, font=("Arial", 25)) score.pack() root.mainloop() main()#call 

Everything works perfectly fine, but it doesn't show my score in the label. It updates it if I print it so technically it works. But in the label it stays 0. Do you now how to fix that?

Thanks in advance!

2 Answers 2

1

In the counter() function, after clicks += 1, add score.config(text=score)

So the final function looks like this:

def counter(): global clicks clicks += 1 score.config(text=score) 

Also, just a suggestion: Avoid importing everything from a module.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem here is that the initialization code is in a function called main(), so clicks and score will not be available to counter().
Yep, works, but I had to make the variable score a global variable and score.config(text=clicks) not score.config(text=score). I'm going to post the correct code as an answer. Tank you!
0

Final function:

def counter(): global clicks clicks += 1 score.config(text=clicks) 

Correct code:

""" author: Elxas866 file: BubbleClicker.py descr.: A cookie clicker-like game """ from tkinter import * from PIL import ImageTk, Image clicks = 0 def counter(): global clicks clicks += 1 score.config(text=clicks) print(clicks) def main(): root = Tk() root.minsize(500, 500) #pixels root.title("Bubble Cliker") bubble = Image.open("assets/Bubble.png") Bubble = ImageTk.PhotoImage(bubble) image = Button(image=Bubble, command=counter) image.pack() image.place(relx=0.5, rely=0.5, anchor=CENTER) global score score = Label(text=clicks, font=("Arial", 25)) score.pack() root.mainloop() main()#call 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.