Images slideshow

This page provides some helpful discussion for getting started with GUI programming in Python.

Moderators: KDoiron, ChrJim, mawe, python

Images slideshow

Postby Jonte79 on Wed Sep 16, 2009 3:10 am

Can someone help me whit my code... i want to have some ideas about how i continue with my slideshow. I have a gui that shows images slides. I want it to show all slides from a list. I have buttons in the GUI that are "prev" and "next" when i press "next" it should ge the next object from the list. That is my problem now...i can't figure it out how i continue with that code. Can someone give me some ideas?

Thanks!


here is the code:
Code: Select all
from Tkinter import *
from PIL import Image, ImageTk
import sys
import Image

class GUIFramework(Frame):
   """ det här är GUI:T"""
   
       
   def __init__(self, master=None):
        global x, can, data_list, showList
       
       
        Frame.__init__(self,master)

        self.lbl_header = Label (height=2,width=60)
        self.lbl_header.pack(fill=BOTH)
       
        can = Canvas ()
        can.pack(fill=BOTH)
       
        self.lbl_text = Text(height=5,width=60,wrap=WORD )
        self.lbl_text.pack(fill=BOTH)

        self.btn_previous = Button(text="Föregående", fg="blue")
        self.btn_previous.pack(side=LEFT,fill=X, expand=YES)

        self.btn_next = Button(text="Nästa", fg="green", command=self.showList)
        #self.btn_next.bind("<Return>", forward())
        self.btn_next.pack(side=LEFT, fill=X, expand=YES)
       

        self.btn_exit = Button(text="Avsluta",fg="red")
        self.btn_exit.pack(side=LEFT,fill=X, expand=YES)

       
       
        self.lbl_header["text"] = "START genom att trycka på nästa"

        data_list = [["Lejon (Panthera leo)", "bilder/lejon.gif","Lejon är en art i släktet Panthera som tillhör familjen kattdjur. "],
            ["Pandan eller jättepandan (Ailuropoda melanoleuca)","bilder/panda.gif","Lorem ipsum dolor..."],
            ["Kejsarpingvin (Aptenodytes forsteri)", "bilder/pingvin.gif", "Är den största och tyngsta nu..."]]

       self.showList()
       
   #def forward(self):
       
       

       
   #def backward(self):
       
   
     
   def showList(self):
       x= 2
           
       #Rubriken
       self.lbl_header["text"] = data_list[x][0]
       #Information
       self.lbl_text.insert(0.0, data_list[x][2])
       #Bilden
       filename = data_list[x][1]
       img = PhotoImage(file=filename)
       can.config(width=img.width(),height=img.height())
       can.create_image(2,2,image=img, anchor=NW)
       can.mainloop()
       
   

if __name__ == "__main__":
    guiFrame = GUIFramework()
    guiFrame.mainloop()
Jonte79
New Python User
New Python User
 
Posts: 10
Joined: Tue Sep 15, 2009 12:39 am

Re: Images slideshow

Postby jessejames on Wed Sep 16, 2009 11:34 am

I am glad you asked this question because it prompted me to go back and look at some of my old PIL code, heres a something for you to look at. I would probably use the tkFileDialog so the user can pick a directory instead of hardcoding it. You also would want to create a ScrolledCanvas class for export, and use "import Tkinter as tk" so you don't pollute so much, but i am just too lazy today :mrgreen:

Just make sure to adjust the "path" so it points to a directory on your machine, or like i said improve this code with tkFileDialog

Code: Select all
from Tkinter import *
import Image, ImageTk
import os


class ImageViewer(Frame):
    def __init__(self):
        Frame.__init__(self, master=None)
        self.images = []
        self.current = 0
       
        path = 'C:\\Users\\owner\\Pictures\\stuff\\'
        for x in [path+x for x in os.listdir(path)]:
            self.images.append(ImageTk.PhotoImage(Image.open(x)))
        print self.images, len(self.images)
           
        self._create_widgets()
        self._load_image(self.images[0])
        self.pack(fill=BOTH, expand=1)

    def _create_widgets(self):
        self.canvas = Canvas(self, bg='gray', highlightthickness=0)
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        xscroll = Scrollbar(self, orient=HORIZONTAL)
        yscroll = Scrollbar(self, orient=VERTICAL)
        xscroll.configure(command=self.canvas.xview)
        yscroll.configure(command=self.canvas.yview)
        self.canvas.config(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
        xscroll.grid(row=1, column=0, sticky='e'+'w')
        yscroll.grid(row=0, column=1, sticky='n'+'s')
        self.canvas['scrollregion'] = (0, 0, 1, 1)
        self.canvas.grid(row=0, column=0, sticky='n'+'s'+'e'+'w')
        f = Frame(self) ;f.grid(row=2, column=0, columnspan=2)
        Button(f, text='<--', command=self.prev_image).pack(side='left', padx=5, pady=5)
        Button(f, text='-->', command=self.next_image).pack(side='left', padx=5, pady=5)
       
    def _load_image(self, image):
        self.canvas.delete(ALL)
        w, h = image.width(), image.height()
        self.canvas.create_image(1,1, anchor=NW, image=image)
        self.canvas['scrollregion'] = (0, 0, w, h)
   
    def next_image(self):
        self.current += 1
        if self.current >= len(self.images)-1:
            self.current = 0
        print 'loading image %d' %self.current
        self._load_image(self.images[self.current])

    def prev_image(self):
        self.current -= 1
        if self.current <= 0:
            self.current = len(self.images)-1
        print 'loading image %d' %self.current
        self._load_image(self.images[self.current])


pv = ImageViewer()
top = pv.winfo_toplevel()
top.state('zoomed')
pv.mainloop()


PS: you could just as well set the scroll region once since all the images are in memory, by setting it to the largest therby saving a few nano's, but i don't mind if my puter has to work a bit.
Just more bangs and whimpers...ok,ok, more whimpers than bangs really.
User avatar
jessejames
Python Guru
Python Guru
 
Posts: 784
Joined: Mon Nov 24, 2008 9:23 pm

Re: Images slideshow

Postby Jonte79 on Thu Sep 17, 2009 4:22 am

Thank you very much for your reply!

It's working exelent. I need to modify it to take from a list that have several objects.
For example:

Code: Select all
data_list = [
["Lejon (Panthera leo)", "bilder/lejon.gif","Lejon är en art i släktet Panthera som tillhör familjen kattdjur. "],
["Pandan eller jättepandan (Ailuropoda melanoleuca)","bilder/panda.gif","Lorem ipsum dolor..."],
  ["Kejsarpingvin (Aptenodytes forsteri)", "bilder/pingvin.gif", "Är den största och tyngsta nu..."]
]


It' swedish text in it and paths to the image.
Jonte79
New Python User
New Python User
 
Posts: 10
Joined: Tue Sep 15, 2009 12:39 am

Re: Images slideshow

Postby jessejames on Thu Sep 17, 2009 12:31 pm

Jonte79 wrote:It's working exelent. I need to modify it to take from a list that have several objects.
For example:


so why not just *flatten* the list...?

PS: i can't read swede.
Just more bangs and whimpers...ok,ok, more whimpers than bangs really.
User avatar
jessejames
Python Guru
Python Guru
 
Posts: 784
Joined: Mon Nov 24, 2008 9:23 pm

Re: Images slideshow

Postby Jonte79 on Thu Sep 17, 2009 2:16 pm

what do you mean?

/Jonny
Jonte79
New Python User
New Python User
 
Posts: 10
Joined: Tue Sep 15, 2009 12:39 am

Re: Images slideshow

Postby waz on Thu Sep 17, 2009 2:30 pm

Code: Select all
>>> data_list = [["Lejon (Panthera leo)", "bilder/lejon.gif", "Lejon är en art i släktet Panthera som tillhör familjen kattdjur. "], ["Pandan eller jättepandan (Ailuropoda melanoleuca)", "bilder/panda.gif", "Lorem ipsum dolor..."], ["Kejsarpingvin (Aptenodytes forsteri)", "bilder/pingvin.gif", "Är den största och tyngsta nu..."]]
>>> for data in data_list:
        print "Here's a path from the list:", data[1]

Here's a path from the list: bilder/lejon.gif
Here's a path from the list: bilder/panda.gif
Here's a path from the list: bilder/pingvin.gif
>>>
҉ 囧 ҉
waz
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1348
Joined: Tue May 05, 2009 7:02 pm


Return to Graphical User Interface

Who is online

Users browsing this forum: No registered users and 1 guest


Sponsored by Dreamlink Web hosting and Traduzioni Rumeno Italiano and ASSP Deluxe for cPanel.