【问题标题】:Change filepath of an image using command使用命令更改图像的文件路径
【发布时间】:2021-01-30 07:23:08
【问题描述】:

我编写了一些代码,它可以计算出生日期并计算正确的中国星座动物(老鼠、狗等)。我想添加一个 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 变量,以便显示的图像从兔子变为老虎

【问题讨论】:

    标签: file tkinter photoimage


    【解决方案1】:

    你可以使用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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 2012-03-30
      相关资源
      最近更新 更多