Reputation: 25
Currently, my code can cycle through a list of images and can display each image fora certain amount of time. However, it only opens a small window to display the image, which doesn't even fill the entire window. I want to display each image full screen, is there any way in which I could do this?
Also, the script only works for GIFs, is there any way in which I could alter it to accept other types of files such as JPEG.
My code is:
from itertools import cycle
import Tkinter as tk
class App(tk.Tk):
def __init__(self, image_files, delay):
tk.Tk.__init__(self)
w, h = self.winfo_screenwidth(), self.winfo_screenheight()
self.overrideredirect(1)
self.geometry("%dx%d+0+0" % (w, h))
self.delay = delay
self.pictures = cycle((tk.PhotoImage(file=image), image)
for image in image_files)
self.picture_display = tk.Label(self)
self.picture_display.pack()
def show_slides(self):
img_object, img_name = next(self.pictures)
self.picture_display.config(image=img_object)
self.title(img_name)
self.after(self.delay, self.show_slides)
def run(self):
self.mainloop()
delay = 3500
image_files = ["/media/pi/RASPBERRY/object1.gif",
"/media/pi/RASPBERRY/object12ndImage.gif",
"/media/pi/RASPBERRY/object13rdImage.gif",
"/media/pi/RASPBERRY/object14thImage.gif"]
app = App(image_files, delay)
app.show_slides()
app.run()
Upvotes: 0
Views: 2873
Reputation: 403
And if I will like put up buttons to move on?
Tkinter.Button(frame, text='Previous picture', command=lambda: move(-1)).pack(side=Tkinter.LEFT)
Tkinter.Button(frame, text='Next picture', command=lambda: move(+1)).pack(side=Tkinter.LEFT)
Tkinter.Button(frame, text='Quit', command=root.quit).pack(side=Tkinter.LEFT)
Upvotes: 1
Reputation: 15226
My example is using Python 3.x however you should have no problem changing your imports to work with Python 2.x.
That said I would personally use a tracking variable to keep track of the index of our photo list. With this we can run everything we need to run from inside the show_slides
method. The main issue you need to address is the resizing of your images. This can be done with PIL
. We can open the image based on the current path then resize it. We then load the resized image to the label. We can also use os.path.basename()
to get the file name to use on our titles. That said currently the way your program is set up no title will be displaced in full screen.
Take a look at the below code and let me know if you have any questions.
from PIL import Image, ImageTk
import tkinter as tk
import os
class App(tk.Tk):
def __init__(self, image_files, delay):
tk.Tk.__init__(self)
self.w = self.winfo_screenwidth()
self.h = self.winfo_screenheight()
self.overrideredirect(1)
self.geometry("%dx%d+0+0" % (self.w, self.h))
self.delay = delay
self.pictures = []
self.track_img_ndex = 0
for img in image_files:
self.pictures.append(img)
self.picture_display = tk.Label(self)
self.picture_display.pack(expand=True, fill="both")
def show_slides(self):
if self.track_img_ndex < len(self.pictures):
x = self.pictures[self.track_img_ndex]
self.track_img_ndex +=1
original_image = Image.open(x)
resized = original_image.resize((self.w, self.h),Image.ANTIALIAS)
new_img = ImageTk.PhotoImage(resized)
self.picture_display.config(image=new_img)
self.picture_display.image = new_img
self.title(os.path.basename(x))
self.after(self.delay, self.show_slides)
else:
print("End of list!")
delay = 3500
image_files = ["/media/pi/RASPBERRY/object1.gif",
"/media/pi/RASPBERRY/object12ndImage.gif",
"/media/pi/RASPBERRY/object13rdImage.gif",
"/media/pi/RASPBERRY/object14thImage.gif"]
app = App(image_files, delay)
app.show_slides()
app.mainloop()
Upvotes: 3