file - 使用命令更改图像的文件路径
问题描述
我写了一些代码,它需要出生日期并计算正确的中国星座动物(老鼠、狗等)。我想添加一个 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)
推荐阅读
- javascript - 如何获取引导开关的值
- batch-file - 如何在 FOR /R 循环中排除 Windows 目录?
- angular - Viewchild 返回未定义
- sql - 如何切割长度未知的字符串的左侧部分?(带sql函数)
- oracle - Oracle:使用自动列表分区和范围子分区
- javascript - 以不同的方式选择 DOM Element 会带来不同的结果
- javascript - 为什么我的 Promise.all().then() 在所述 Promise.all() 中的承诺之前执行
- php - 运行 PHP 7.1 时如何修复致命错误
- sql - 计算表中所有列中的空值和非空值
- php - 在 Laravel 中获取当前语言