Python 更改按钮图像并恢复
我有一些代码,可以在点击按钮时改变它的图片。现在,我想要的是:按钮在被点击时改变图片,然后再恢复到正常状态(就像普通按钮那样)。我不能简单地再加一个 .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)
1 个回答
0
让按钮的图片看起来像真的被按下去一样,最好的方法是这样做。
定义你的图片:
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)