Skip to content

Instantly share code, notes, and snippets.

@lqc
Created December 12, 2009 05:22
Show Gist options
  • Save lqc/254744 to your computer and use it in GitHub Desktop.
Save lqc/254744 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import Tkinter as tk
import sys
import os
from PIL import Image, ImageTk
class SlideshowApp(tk.Frame):
def __init__(self, master = None):
tk.Frame.__init__(self, master)
self.pack()
self.current_image_index = 0
self.image_list = []
self.delay = 3000 # 3 seconds
self.timer_id = None
self._create_widgets()
def _create_widgets(self):
self.CANVAS = tk.Canvas(self, bd = 2, width = 800, height = 600, bg = 'black')
self.CANVAS.grid(column = 0, row = 0, columnspan = 2)
self.START = tk.Button(self, text = "Rozpocznij pokaz", command = self.start_slideshow)
self.START.grid(column = 0, row = 1, sticky = tk.E + tk.W)
self.QUIT = tk.Button(self, text = "Zamknij", command = self.quit)
self.QUIT.grid(column = 1, row = 1, sticky = tk.E + tk.W)
def start_slideshow(self):
if self.timer_id is not None:
return
if len(self.image_list) != 0:
self.current_image_index = -1
self.next_image()
def next_image(self):
self.current_image_index = (self.current_image_index + 1) % len(self.image_list)
try:
image = Image.open(self.image_list[self.current_image_index])
image = self.scale_if_needed(image, 800, 600)
photo = ImageTk.PhotoImage(image)
self.CANVAS.create_image(400, 300, image = photo)
self.current_image = photo # keep the references
except tk.TclError, er:
print "TCL exception:", e
self.timer_id = self.after(self.delay, self.next_image)
def scale_if_needed(self, image, w, h):
iw, ih = image.size
if iw > w and iw > ih:
scale = w * 1.0 / iw
elif ih > h and ih > iw:
scale = h * 1.0 / ih
else:
return image
return image.resize((int(iw * scale), int(ih * scale)))
if len(sys.argv) == 1:
print "Użycie: ./slideshow.py <obrazek1> ... <obrazekN>"
sys.exit(1)
root = tk.Tk()
app = SlideshowApp(master = root)
app.image_list = sys.argv[1:]
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment