【问题标题】:Python Change Button Image and change it backPython更改按钮图像并将其改回
【发布时间】:2014-04-22 23:58:15
【问题描述】:

所以我有一些代码可以在单击时更改按钮的图像。现在,如果我只想在单击按钮时更改按钮然后恢复正常怎么办? (就像普通按钮一样)我不能只添加另一个 .config 方法,因为它会跳过第一个并保持按钮相同的图像。我也不能等待,因为它只是冻结程序并产生相同的结果。

PlayUp = PhotoImage(file=currentdir+'\Up_image.gif')
PlayDown = PhotoImage(file=currentdir+'\Down_image.gif')
#Functions
def playButton():
    pButton.config(image=PlayDown)
    return

#Play Button
pButton = Button(root, text="Play", command=playButton)
pButton.grid(row=1)
pButton.config(image=PlayUp)

【问题讨论】:

  • 字符串\U\D 并不代表您认为的那样。尝试r'\Up_image.gif' 以获得更好的结果(或者最好仍然使用 os.path.join)。由于您没有说明您使用的是哪个小部件库,因此很难猜测是否还有其他问题。
  • 好的,这些是我连接的图像路径。不管怎样,我试过你说的,它仍然会改变,但不会变回来。

标签: python image button


【解决方案1】:

使按钮图像看起来像真正的按钮按下但使用图像的最佳方法是执行类似的操作。

定义您的照片:

PlayUp = PhotoImage(file=currentdir+r'\Up_image.gif')
PlayDown = PhotoImage(file=currentdir+r'\Down_image.gif')

定义你的功能:

def returncolor():
    pButton.config(image=PlayUp)
def playButton():
    pButton.config(image=PlayDown)
    pButton.after(200,returncolor)

初始化按钮:

#Play Button
pButton = Button(root, text="Play", command=playButton, borderwidth=0)
pButton.grid(row=1)
pButton.config(image=PlayUp)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    相关资源
    最近更新 更多