Thursday, July 26, 2012

Tkinter Listbox Colours Demo

This code is based on the Tcl colors.tcl demo. Note that the colours you get are not always those you'd think you'd see given the colour name. You may also want to check out John Grayson's colorswatch.py



# File: listboxcolors.py
# References:
#    http://infohost.nmt.edu/tcc/help/pubs/tkinter//listbox.html
#    http://www.tcl.tk/man/tcl8.5/TkCmd/listbox.htm
#    http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_scrollbar.htm

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

COLORS = ('AntiqueWhite1', 'AntiqueWhite2', 'AntiqueWhite3', 'AntiqueWhite4', 
          'CadetBlue1', 'CadetBlue2', 'CadetBlue3', 'CadetBlue4', 
          'DarkGoldenrod1', 'DarkGoldenrod2', 'DarkGoldenrod3', 'DarkGoldenrod4', 
          'DarkOliveGreen1', 'DarkOliveGreen2', 'DarkOliveGreen3', 'DarkOliveGreen4', 
          'DarkOrange1', 'DarkOrange2', 'DarkOrange3', 'DarkOrange4', 
          'DarkOrchid1', 'DarkOrchid2', 'DarkOrchid3', 'DarkOrchid4', 
          'DarkSeaGreen1', 'DarkSeaGreen2', 'DarkSeaGreen3', 'DarkSeaGreen4', 
          'DarkSlateGray1', 'DarkSlateGray2', 'DarkSlateGray3', 'DarkSlateGray4', 
          'DeepPink1', 'DeepPink2', 'DeepPink3', 'DeepPink4', 
          'DeepSkyBlue1', 'DeepSkyBlue2', 'DeepSkyBlue3', 'DeepSkyBlue4', 
          'DodgerBlue1', 'DodgerBlue2', 'DodgerBlue3', 'DodgerBlue4', 
          'HotPink1', 'HotPink2', 'HotPink3', 'HotPink4', 
          'IndianRed1', 'IndianRed2', 'IndianRed3', 'IndianRed4', 
          'LavenderBlush1', 'LavenderBlush2', 'LavenderBlush3', 'LavenderBlush4', 
          'LemonChiffon1', 'LemonChiffon2', 'LemonChiffon3', 'LemonChiffon4', 
          'LightBlue1', 'LightBlue2', 'LightBlue3', 'LightBlue4', 
          'LightCyan1', 'LightCyan2', 'LightCyan3', 'LightCyan4', 
          'LightGoldenrod1', 'LightGoldenrod2', 'LightGoldenrod3', 'LightGoldenrod4', 
          'LightPink1', 'LightPink2', 'LightPink3', 'LightPink4', 
          'LightSalmon1', 'LightSalmon2', 'LightSalmon3', 'LightSalmon4', 
          'LightSkyBlue1', 'LightSkyBlue2', 'LightSkyBlue3', 'LightSkyBlue4', 
          'LightSteelBlue1', 'LightSteelBlue2', 'LightSteelBlue3', 'LightSteelBlue4', 
          'LightYellow1', 'LightYellow2', 'LightYellow3', 'LightYellow4', 
          'MediumOrchid1', 'MediumOrchid2', 'MediumOrchid3', 'MediumOrchid4', 
          'MediumPurple1', 'MediumPurple2', 'MediumPurple3', 'MediumPurple4', 
          'MistyRose1', 'MistyRose2', 'MistyRose3', 'MistyRose4',
          'NavajoWhite1', 'NavajoWhite2', 'NavajoWhite3', 'NavajoWhite4', 
          'OliveDrab1', 'OliveDrab2', 'OliveDrab3', 'OliveDrab4', 
          'OrangeRed1', 'OrangeRed2', 'OrangeRed3', 'OrangeRed4', 
          'PaleGreen1', 'PaleGreen2', 'PaleGreen3', 'PaleGreen4', 
          'PaleTurquoise1', 'PaleTurquoise2', 'PaleTurquoise3', 'PaleTurquoise4', 
          'PaleVioletRed1', 'PaleVioletRed2', 'PaleVioletRed3', 'PaleVioletRed4', 
          'PeachPuff1', 'PeachPuff2', 'PeachPuff3', 'PeachPuff4', 
          'RosyBrown1', 'RosyBrown2', 'RosyBrown3', 'RosyBrown4', 
          'RoyalBlue1', 'RoyalBlue2', 'RoyalBlue3', 'RoyalBlue4', 
          'SeaGreen1', 'SeaGreen2', 'SeaGreen3', 'SeaGreen4', 
          'SkyBlue1', 'SkyBlue2', 'SkyBlue3', 'SkyBlue4', 
          'SlateBlue1', 'SlateBlue2', 'SlateBlue3', 'SlateBlue4', 
          'SlateGray1', 'SlateGray2', 'SlateGray3', 'SlateGray4', 
          'SpringGreen1', 'SpringGreen2', 'SpringGreen3', 'SpringGreen4', 
          'SteelBlue1', 'SteelBlue2', 'SteelBlue3', 'SteelBlue4', 
          'VioletRed1', 'VioletRed2', 'VioletRed3', 'VioletRed4', 
          'aquamarine1', 'aquamarine2', 'aquamarine3', 'aquamarine4', 
          'azure1', 'azure2', 'azure3', 'azure4', 
          'bisque1', 'bisque2', 'bisque3', 'bisque4', 
          'blue1', 'blue2', 'blue3', 'blue4', 
          'brown1', 'brown2', 'brown3', 'brown4', 
          'burlywood1', 'burlywood2', 'burlywood3', 'burlywood4', 
          'chartreuse1', 'chartreuse2', 'chartreuse3', 'chartreuse4', 
          'chocolate1',  'chocolate2', 'chocolate3', 'chocolate4', 
          'coral1', 'coral2', 'coral3', 'coral4', 
          'cornsilk1', 'cornsilk2', 'cornsilk3', 'cornsilk4', 
          'cyan1', 'cyan2', 'cyan3', 'cyan4', 
          'firebrick1', 'firebrick2','firebrick3', 'firebrick4', 
          'gold1', 'gold2', 'gold3', 'gold4', 
          'goldenrod1', 'goldenrod2', 'goldenrod3', 'goldenrod4',
          'gray60', 'gray70', 'gray80', 'gray85', 'gray90', 'gray95', 
          'green1', 'green2', 'green3', 'green4', 
          'honeydew1', 'honeydew2', 'honeydew3', 'honeydew4', 
          'ivory1', 'ivory2', 'ivory3', 'ivory4', 
          'khaki1', 'khaki2', 'khaki3', 'khaki4', 
          'magenta1', 'magenta2', 'magenta3', 'magenta4', 
          'maroon1', 'maroon2', 'maroon3', 'maroon4', 
          'orange1', 'orange2', 'orange3', 'orange4', 
          'orchid1', 'orchid2', 'orchid3', 'orchid4', 
          'pink1', 'pink2', 'pink3', 'pink4', 
          'plum1', 'plum2', 'plum3', 'plum4', 
          'purple1', 'purple2', 'purple3', 'purple4', 
          'red1', 'red2', 'red3', 'red4', 
          'salmon1', 'salmon2', 'salmon3', 'salmon4', 
          'seashell1', 'seashell2', 'seashell3', 'seashell4', 
          'sienna1', 'sienna2', 'sienna3', 'sienna4', 
          'snow1', 'snow2', 'snow3', 'snow4', 
          'tan1', 'tan2', 'tan3', 'tan4', 
          'thistle1', 'thistle2', 'thistle3', 'thistle4', 
          'tomato1', 'tomato2', 'tomato3', 'tomato4', 
          'turquoise1', 'turquoise2', 'turquoise3', 'turquoise4', 
          'wheat1', 'wheat2', 'wheat3', 'wheat4', 
          'yellow1', 'yellow2', 'yellow3', 'yellow4')

class ListboxColorsDemo(ttk.Frame):
    
    def __init__(self, isapp=True, name='listboxcolorsdemo'):
        ttk.Frame.__init__(self, name=name)
        self.pack(expand=Y, fill=BOTH)
        self.master.title('Listbox Colors Demo')
        self.isapp = isapp
        self._create_widgets()
        
    def _create_widgets(self):
        if self.isapp:
            MsgPanel(self, 
                     ["A listbox containing several colour names is displayed below, along ",
                      "with a scrollbar.  You can scan the list using the scrollbar ",
                      "or by holding down the left mouse button while dragging it within ",
                      "the listbox window.\n\n",
                      "Double-click a colour to change the demo panel background colour."])
            
            SeeDismissPanel(self)
        
        self._create_demo_panel()
        
    def _create_demo_panel(self):
        # use custom style
        demoPanel = ttk.Frame(self, style='Demo.TFrame')
        demoPanel.pack(side=TOP, fill=BOTH, expand=Y)
                
        self._colors_lb(demoPanel)        
                
    def _colors_lb(self, parent):
        # use custom style
        f = ttk.Frame(parent, borderwidth='.5c', style='Demo.TFrame')
        sb = ttk.Scrollbar(orient=VERTICAL)
        
        # 'setgrid' allows the widget to
        # control the resize dimension (i.e. character
        # vs pixel) used by the grid in its top-level window
        # 'width' = number of characters
        # 'height' = number of lines (not pixels)
        self.lb = Listbox(setgrid=True, width=20, height=12)        
        sb['command'] = self.lb.yview
        self.lb['yscroll'] = sb.set
        
        self.lb.insert(END, *COLORS)
        
        self.lb.bind('<Double-1>', self._color_changed)

        sb.pack(in_=f, side=RIGHT, fill=Y)
        self.lb.pack(in_=f, side=LEFT, fill=BOTH, expand=Y)

        f.pack()

    def _color_changed(self, *args):
        # modify background of custom style
        ttk.Style().configure('Demo.TFrame', 
                               background=self.lb.selection_get())


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