[Python] How to change Tkinter button image on click?

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have two images for a single button and also I created buttons with images. I would like to change the button image whenever I first click it. For example when I click the button I want button image to change "UnclickedImage" to "ClickedImage".

    I could not be able to find any similar question in stackoverflow.

    from tkinter import *


    class Example(Frame):

    def __init__(self, tk=None):
    super().__init__()
    self.tk = tk
    self.init_ui()

    def init_ui(self):

    self.photoTodo = PhotoImage(file="./Images/TodoUnClicked.png")
    Button(self.tk, image=self.photoTodo).pack(side=LEFT)
    self.photoStayFocussed = PhotoImage(file="./Images/StayFoccusedUnClicked.png")
    Button(self.tk, image=self.photoStayFocussed).pack(side=RIGHT)


    def main():

    root = Tk()
    root.configure(bg='white')
    ex = Example(root)
    root.title('')
    root.iconbitmap('./Images/empty.ico')
    root.geometry("400x100+300+300")
    root.mainloop()


    if __name__ == '__main__':
    main()

    Continue reading...

Compartilhe esta Página