Home > OS >  Change back to unclicked state - Tkinter Python
Change back to unclicked state - Tkinter Python

Time:01-08

So i am building this desktop app with Tkinter and Python and i am messing around with a like button, and i want when the button is clicked the like (in this case a heart) fills up with color, which i have already achieve by giving a command to the button to .configure a new image (command= lambda: button_like.config(image=self.img_like_pressed)) But now, how can i make it if the user decides to "unlike" the thing, the button goes back to its original state. I am thinking maybe with a function, but i have already tried to do a various number of things and nothing works. Maybe i should pass the (command= lambda: button_like.config(image=self.img_like_pressed)) inside a function that detects when the button is pressed right? But how? Any help is very much appreciated!

Here's my code for the class where i am configuring out the like button:

class One_Recipe(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="One Recipe", font=LARGE_FONT) #Label is the class and i created the object it is in
        label.pack()

        self.value = False
        def change_value():
            if self.value is False:
                self.value = False 
                print("button not pressed")
            if self.value is True:
                self.value = True
                print("button pressed")

        c_recipe = Canvas(self, width=513, height=513, highlightthickness=0,borderwidth=0,relief="flat", background="#FFC024",)
        c_recipe.place(x=857, y=256,)
        self.img_placer = PhotoImage(file="assets/frame0/recipe/placer.png")
        c_recipe.create_image(0, 0,anchor="nw", image= self.img_placer)
        
        self.img_like = PhotoImage(file = r"assets/frame0/heart.png")
        self.img_like_pressed = PhotoImage(file = r"assets/frame0/heart_pressed.png")
        button_like = tk.Button(self, image = self.img_like, command= lambda: button_like.config(image=self.img_like_pressed), relief="flat", borderwidth=0, highlightthickness=0,)
        button_like.place(x=1344, y=218, width=27, height=23)```

CodePudding user response:

As you rightly suggest, rather than just passing command a lambda function, you can instead pass a class method, e.g.,:

class One_Recipe(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="One Recipe", font=LARGE_FONT) #Label is the class and i created the object it is in
        label.pack()

        self.value = False
        def change_value():
            if self.value is False:
                self.value = False 
                print("button not pressed")
            if self.value is True:
                self.value = True
                print("button pressed")

        c_recipe = Canvas(self, width=513, height=513, highlightthickness=0,borderwidth=0,relief="flat", background="#FFC024",)
        c_recipe.place(x=857, y=256,)
        self.img_placer = PhotoImage(file="assets/frame0/recipe/placer.png")
        c_recipe.create_image(0, 0,anchor="nw", image= self.img_placer)
        
        self.img_like = PhotoImage(file = r"assets/frame0/heart.png")
        self.img_like_pressed = PhotoImage(file = r"assets/frame0/heart_pressed.png")
        
        # make button_like a class attribute and pass command the flip_like method
        self.button_like = tk.Button(self, image = self.img_like, command=self.flip_like, relief="flat", borderwidth=0, highlightthickness=0,)
        self.has_liked = False  # variable to check whether liked or not
        self.button_like.place(x=1344, y=218, width=27, height=23)

    def flip_like(self):
        """
        Flip the like button between a liked and unliked state on
        a button press.
        """

        if not self.has_liked:
            # set like
            self.button_like.config(image=self.img_like_pressed)
            self.has_liked = True
        else:
            # unset like
            self.button_like.config(image=self.img_like)
            self.has_liked = False
            
  • Related