我无法在tkinter Python中更改按钮图像

2024-10-02 00:23:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,当然这有一个简单的答案。非常感谢您的帮助,这是我的代码:)

    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)

Tags: 答案代码imageloopplayifpngdef
1条回答
网友
1楼 · 发布于 2024-10-02 00:23:24

请参考此代码并将其更改为您的样式:

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()

这是输出: enter image description here

enter image description here

相关问题 更多 >

    热门问题