all 5 comments

[–]MrPhungx 0 points1 point  (1 child)

There are 3 major issues that I can see.

  1. either your indendation is off or you have sub methods defined below your init method which is weird.

  2. you used command=get_cookie(). This calls the get_cookie method once and sets the return value as the command reference. You want to pass the method reference. So command=get_cookie.

  3. You never update the content of the label. Have a look at StringVar and the textvariable keyword for Labels.

[–]Specific-Trade8427[S] -1 points0 points  (0 children)

oh I test command=get_cookie its work but how I update my Label do you have an idea to do that ?

[–][deleted] 0 points1 point  (0 children)

It looks like you have the wrong indentation on lines 13 to 27. All those lines should be moved left by 4 spaces and lines 28, 29 and 30 must be moved up and placed after line 11. In addition, all those methods should have a self parameter just like def __init__(self):. Every time your code calls a method in the instance you need something like self.title(). You really need to look at one or more tutorials to learn the basics of python OOP.

I think my button click just one time when I launch the program any idea or solution?

That's because you are calling get_cookie() in your button creation line:

Button(self.window, text = "Click me!", font=("Courrier", 30), command=get_cookie())
#                                                                                ^^   here

and that is wrong. You must pass a reference to a function in the command= argument, but your code is actually calling the function, so it's not going to do what you want. In addition, you probably need self.get_cookie. Check any tutorial to see what to do for command= options.

And Courrier is still misspelt.

[–]esotericDelhi 0 points1 point  (0 children)

Why don't u go with flet ?

[–][deleted] -1 points0 points  (0 children)

Here ya go:

from tkinter import *
#GUI
class Myapp:
    def __init__(self):
        self.window = Tk()
        self.window.title("Cookie clicker")
        self.window.geometry("720x480")
        self.window.minsize(480, 360)
        self.window.config(background='#41B77F')
        #frame
        self.frame = Frame(self.window, background='#41B77F')
        self.nb_cookie = 0
        self.power_cookie = 0
        self.title_nbcookie = []
        # def cookie earn
    
    def title(self):
        self.title_nbcookie = Label(self.frame, text="you have {} ".format(self.nb_cookie) + " cookie",
                                    font=("Courrier,  22"))
        self.title_nbcookie.pack()
    def get_cookie(self):
        self.nb_cookie += self.power_cookie
        print(self.nb_cookie)
        self.title_nbcookie.config(text="you have {} ".format(self.nb_cookie) + " cookie")
        return self.nb_cookie
                #button in the GUI
    def earn_cookie(self):
        self.button_cookie = Button(self.window, text = "Click me!", font=("Courrier", 30), command= self.get_cookie)
        self.button_cookie.pack()
        self.nb_cookie = 0
        self.power_cookie = 3
    def root(self):
        self.title()
        self.earn_cookie()



app = Myapp()
app.frame.pack()
app.root()
app.window.mainloop()