Page List

Tuesday, July 24, 2012

Tkinter Image Labels Demo

This code is based on the Tcl image1.py demo. Note that if you want to show non-gif images you'll need to use the Python Imaging Library (PIL ... see the code in Tkinter Demos for an example of usage.) A Python 3 version is available from the University of California, Python Extensions site.


# File: imagelabels.py
# References:
#    http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_label.htm

from tkinter import *
from tkinter import ttk
from demopanels import MsgPanel, SeeDismissPanel

class ImageLabelsDemo(ttk.Frame):
    
    def __init__(self, isapp=True, name='imagelabeldemo'):
        ttk.Frame.__init__(self, name=name)
        self.pack(expand=Y, fill=BOTH)
        self.master.title('Image Labels Demo')
        self.isapp = isapp
        self._create_widgets()
        
    def _create_widgets(self):
        if self.isapp:
            MsgPanel(self, ["This demonstration displays two images, each in a separate label widget."])
            
            SeeDismissPanel(self)
        
        self._create_demo_panel()
        
    def _create_demo_panel(self):
        demoPanel = Frame(self)
        demoPanel.pack(side=TOP, fill=BOTH, expand=Y)
        
        im = PhotoImage(file='images//earth.gif')
        lbl = ttk.Label(demoPanel, image=im, relief=SUNKEN, border=2)
        lbl.image = im
        lbl.pack(side=TOP, padx='.5m', pady='.5m')
        
        im = PhotoImage(file='images//earthris.gif')
        lblb = ttk.Label(demoPanel, image=im, relief=SUNKEN, border=2)
        lblb.image = im
        lblb.pack(side=TOP, padx='.5m', pady='.5m')


if __name__ == '__main__':
    ImageLabelsDemo().mainloop()