0

I asked this question before and the previous one linked to another question but it didn't solve the problem.

I have a number of images, which are updated frequently in a file. The names of images are on the same pattern (e.g: img1.jpg , img2.jpg ...)

I am programming a GUI on python tkinter to start from the first image and automatically update after 30 second. after many tries, I have no idea how to achieve that because mainloop()function freezes the GUI and the problem is that I need them in one GUI.

Here is the code I used to open and resize images:

from PIL import Image, ImageTk

x=1
class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.pack(fill=BOTH, expand=1)
        load = Image.open("./images/img%s.jpg"%x)
        load=load.resize((1350,800))
        render = ImageTk.PhotoImage(load)
        img = Label(self, image=render)
        img.image = render
        img.place(x=0, y=0)
        
root = Tk()
app = Window(root)
root.wm_title("Tkinter window")
root.geometry("1350x800")
root.mainloop() 

I tried to use label.after(3000,function) but no results came up so I deleted it from the code. Is there any way to solve this problem?

Thanks in advance

Grayrigel
  • 3,474
  • 5
  • 14
  • 32

2 Answers2

1

Too big for the comments, But this is a working version of your code:

from tkinter import *
from PIL import Image, ImageTk


class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.pack(fill=BOTH, expand=1)

        self.x = 1 #assigning x to 1 to start with img1 
        self.load = Image.open(f"./images/img{self.x}.jpg").resize((1350,800),Image.ANTIALIAS) #loading first img
        self.render = ImageTk.PhotoImage(self.load) #instantiating first img
        self.img = Label(self, image=self.render) #labelizing it
        self.img.image = self.render #keeping reference
        self.img.pack() #packing it because place(x=0,y=0) will lead to overwriting of image
        self.master.after(3000,self.update) #calling the function continoursly
    def update(self):
        self.x+=1 #increasing the x by 1 over each call to function
        if self.x < 9:
            self.load = Image.open(f"./images/img{self.x}.jpg").resize((1350,800),Image.ANTIALIAS)
            self.render = ImageTk.PhotoImage(self.load)
            self.img.config(image=self.render) #changing the image of label
            self.master.after(3000,self.update)

root = Tk()
app = Window(root)
root.wm_title("Tkinter window")
root.geometry("1350x800")
root.mainloop() 

Hope this helped, do let me know if any errors or doubts.

Cheers

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
0

This code solved my problem:

#from tkinter import *
from PIL import Image, ImageTk
import os
from tkinter import Frame,Label,BOTH,Tk
class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.pack(fill=BOTH, expand=1)

        self.x = 1
        self.load = Image.open(f"./images/img{self.x}.jpg").resize((1350,800),Image.ANTIALIAS)
        self.render = ImageTk.PhotoImage(self.load)
        self.img = Label(self, image=self.render)
        self.img.image = self.render
        self.img.pack()
        self.master.after(3000,self.update)
    def update(self):
        if self.x<len(os.listdir("./images/")):

            self.x+=1
            self.load = Image.open(f"./images/img{self.x}.jpg").resize((1350,800),Image.ANTIALIAS)
            self.render = ImageTk.PhotoImage(self.load)
            self.img.config(image=self.render)
            self.master.after(3000,self.update)

        else:
            self.x=1
            self.master.after(1,self.update)
root = Tk()
app = Window(root)
root.wm_title("Tkinter window")
root.geometry("1350x800")
root.mainloop()