Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

I can't change the button image in tkinter Python

I am new to Python and for sure this has a simple answer. I would appreciate your help and here is my code :)

    def Button1B():
        if button1['image'] == play:
            button1['image'] = loop
    play=PhotoImage(file="play.png")
    loop=PhotoImage(file="loop.png")
    button1=Button(frame, image=play, bg="#292929", bd=0, activebackground="#292929", cursor="hand2", command=Button1B)
    button1.place(x=15, y=450)
question from:https://stackoverflow.com/questions/65878172/i-cant-change-the-button-image-in-tkinter-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Please refer to this code and change it to your style:

def button_change():
    button1.configure(image=loop_image)


frame = Tk()
frame.title('image')

play_image = PhotoImage(file="start.png")
loop_image = PhotoImage(file="stop.png")

button1 = Button(frame, image=play_image, command=button_change)
button1.place(x=15, y=450)

frame.mainloop()

This is output: enter image description here

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...