【问题标题】:I can't change the button image in tkinter Python我无法在 tkinter Python 中更改按钮图像
【发布时间】:2021-01-25 02:18:42
【问题描述】:

我是 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)

【问题讨论】:

  • 如果你在创建play之后执行print(play),你会得到类似pyimage的东西。所以 if 语句总是被评估为 False。

标签: python image tkinter


【解决方案1】:

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

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

这是输出:

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 2018-09-27
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    相关资源
    最近更新 更多