如何解决使用命令更改图像的文件路径
我已经写了一些代码,它获取出生日期并计算正确的中国星座动物(鼠、狗等)。我想添加一个 gui,当用户点击计算时,gui 会显示适当动物的照片。为此,我想我必须在命令定义中更改图像的文件路径,但找不到方法来做到这一点。我已经尽可能简化了下面的代码:
x = Tk()
x.state('zoomed')
def find_animal():
animal = 'tiger' # I've left out the actual calculation part
picture.place(relx=0.4,rely=0.5)
b = Button(x,text='Calculate',command=find_animal)
b.place(relx=0.5,rely=0.3)
file_path = 'rabbit.png'
pic = PhotoImage(file=file_path)
picture = Label(x,image=pic)
x.mainloop()
我希望做的事情是以某种方式更改 find_animal 函数中的 file_path 变量,以便显示的图像从兔子变为老虎
解决方法
您可以使用 pic.config()
更改 find_animal()
内的图像路径:
def find_animal():
animal = 'tiger' # I've left out the actual calculation part
pic.config(file=animal+".png")
picture.place(relx=0.4,rely=0.5)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。