Python Forum
[Tkinter] Tkinter don't change the image
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter don't change the image
#1
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. Smile
Reply
#2

  1. Python 2.7 reached end of life January 1, 2000 see: https://www.python.org/doc/sunset-python-2/
  2. in python 3, you must change from Tkinter import *
    To: from tkinter import * (change case of tkinter)
    better to import specific modules, but won't cause an issue.
  3. (line 3) is never used
  4. pathlib is not supported by python 2.7, requires 3.6 or newer
  5. img1 (line 13) is never used

I'd suggest looking at a sample and retrying (random example: https://www.activestate.com/resources/qu...n-tkinter/)
Reply
#3
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()
BashBedlam likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 3,654 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,193 Aug-02-2022, 09:13 PM
Last Post: woooee
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,420 May-23-2022, 03:44 PM
Last Post: bigmac
  Can't get tkinter button to change color based on changes in data dford 4 3,265 Feb-13-2022, 01:57 PM
Last Post: dford
  tkinter change the text of the checkbox zazas321 1 3,666 Sep-17-2021, 06:19 AM
Last Post: zazas321
  How to redraw an existing image to a different image TkInter zazas321 6 5,586 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,445 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 3,859 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 17,284 Feb-06-2021, 03:35 PM
Last Post: joe_momma
  Latex image too big on tkinter using sympy 4096 1 2,822 Nov-05-2020, 08:05 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020