![]() |
[Tkinter] Tkinter don't change the image - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Tkinter don't change the image (/thread-37788.html) |
Tkinter don't change the image - DQT - Jul-22-2022 Hello everyone. I am using Python 2.7(if it nescessary). Here is my code from Tkinter import * from PIL import Image, ImageTk from os.path import exists from pathlib import Path root=Tk(); root.geometry("500x500"); lab=Label(root,font="bold"); lab.pack(); file_dir = "bk.png"; img1=ImageTk.PhotoImage(Image.open(file_dir)) def img_reset(): #Load img if(Path("a.png").is_file): lab.config(image=ImageTk.PhotoImage(file=Path("a.png"))); def move(): global lab; root.after(200,img_reset); root.after(200,move); move(); root.mainloop();The code don't change lab 's image to "a.png", it still "bk.png"(line 19) Thank you for helped me. ![]() RE: Tkinter don't change the image - Larz60+ - Jul-22-2022
I'd suggest looking at a sample and retrying (random example: https://www.activestate.com/resources/quick-reads/how-to-add-images-in-tkinter/) RE: Tkinter don't change the image - menator01 - Jul-22-2022 As Larz60+ recommended upgrade to python3. After the recommended link, here is a snippet to work with. import tkinter as tk from tkinter import PhotoImage def change_img(parent, label, images): if label['image'] == str(images[0]): label['image'] = str(images[1]) else: label['image'] = 'pyimage1' parent.after(3000, lambda: change_img(parent, label, images)) root = tk.Tk() img1 = PhotoImage(file='path/to/image1') img2 = PhotoImage(file='path/to/image2') label = tk.Label(root) label['image'] = img1 label.pack() img1.image = img1 img2.image = img2 root.after(3000, lambda:change_img(root, label, (img1, img2))) root.mainloop() |