Home > Net >  Is there any way to change the button image while clicking/the button is active in Tkinter Python?
Is there any way to change the button image while clicking/the button is active in Tkinter Python?

Time:08-20

There are ways to change the foreground and background with activebackground and activeforeground, but activeimage doesn't exist - that or it doesn't work for me

Working button changing background and foreground while clicking:

OneP = Button(text="1P", font=("Ink Free", 300), bg="white", activebackground="white", activeforeground="#6f56b3", borderwidth=0)

Button not working:

accept = Button(image=tick, activeimage=purpleTick, bg="white", borderwidth=0, command=acceptClicked)

CodePudding user response:

The tkinter Button widget doesn't support that functionality. I would suggest you to use the .bind method of the button widget. With that method you can bind special built in events (as described in the code) to a button.

your_button = Button(root, bg="white", ...)


# "<Button-1>" is an event which is fired, 
# once the left mouse button is being pressed down on top of the your_button widget
# Therefore show your background image (=> Configure the button to show it)

your_button.bind("<Button-1>", lambda: your_button.configure(image=your_image))  


# "<ButtonRelease-1>" is an event which is fired, 
# once the left mouse button is being released on top of the your_button widget
# Therefore hide the background image

your_button.bind("<ButtonRelease-1">, lambda: your_button.configure(image=your_image)) 

CodePudding user response:

The Tk Button Widget Reference Manual doesn't have the attribute activeimage that you're passing to OneP button. There's no function acceptClicked too, so you need to define it first. I also set the variable tick pointing to an image 'tick.png'.

Below a fully working code following the piece you posted:

from tkinter import *
from tkinter import messagebox
from tkinter import ttk

# Create a window
root = Tk()

# Title of window 
root.title("Button click example")

# Set the resolution of window
root.geometry("500x300 10 10")
 
# Allow Window to be resizable
root.resizable(width = True, height = True)

# Variable type Image 'tick'
tick = PhotoImage(file='tick.png')

def acceptClicked():
    answer = messagebox.showinfo(message='Your answer was accepted!')
    return answer

OneP = Button(root, text="1P", font=("Ink Free", 100), bg="white", activebackground="white", activeforeground="#6f56b3", borderwidth=0)
OneP.grid(row=1,column=0)

accept = Button(root, image=tick, bg="white", borderwidth=0, command=acceptClicked)
accept.grid(row=2,column=0)

root.mainloop()

I only changed the font size of OneP to 100 and added it to a grid as well button accept.

For more details of use of Button widget I realy recomend you read the docs Button Widget

Important: To correctly guide and help you I recommend you post your full code or a piece that helps others understand what you're really attending to do.

  • Related