Friday, August 31, 2012

Tkinter Built-in Bitmap Demo

This code is based on the Tcl bitmap.tcl demo. The demo displays the built-in bitmaps available on all systems; a number of other are available for Macs.


# File: bitmap.py

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

class BitmapDemo(ttk.Frame):
    
    def __init__(self, isapp=True, name='bitmapdemo'):
        ttk.Frame.__init__(self, name=name)
        self.pack(expand=Y, fill=BOTH)
        self.master.title('Bitmap Demo')
        self.isapp = isapp
        self._create_widgets()
        
    def _create_widgets(self):
        if self.isapp:
            MsgPanel(self, 
                     ["This window displays all of Tk's built-in bitmaps, ",
                      "and their calling names."])
            
            SeeDismissPanel(self)
        
        self._create_demo_panel()
        
    def _create_demo_panel(self):
        demoPanel = Frame(self)
        demoPanel.pack(side=TOP, fill=BOTH, expand=Y)
        
        # the following are available on all systems; on Macs a number of
        # other built-in maps are also available
        # see: http://www.tcl.tk/man/tcl8.5/TkLib/GetBitmap.htm
        self._bitmap_row(demoPanel, *('error', 'gray12', 'gray25', 'gray50', 'gray75'))
        self._bitmap_row(demoPanel, *('hourglass', 'info', 'question',
                                     'questhead', 'warning'))
        
    def _bitmap_row(self, parent, *args):
        # creates a row of built-in bitmaps
        f = ttk.Frame(parent)
        f.pack(side=TOP, fill=BOTH)
        
        for item in args:
            bf = ttk.Frame(f)
            bf.pack(side=LEFT, fill=BOTH, pady='.25c', padx='.25c')
            
            # the built-in bitmaps don't play nicely with ttk.Label            
            bm = Label(bf, bitmap=item)     
            txt = ttk.Label(bf, text=item, width=9)
            txt.pack(side=BOTTOM)
            bm.pack(side=BOTTOM)
            
if __name__ == '__main__':
    BitmapDemo().mainloop()