PageRenderTime 65ms CodeModel.GetById 54ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Demo/tix/samples/SHList2.py

http://unladen-swallow.googlecode.com/
Python | 168 lines | 80 code | 40 blank | 48 comment | 6 complexity | 6252c743a12d26a66462168937b71595 MD5 | raw file
  1# -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2#
  3# $Id: SHList2.py 36560 2004-07-18 06:16:08Z tim_one $
  4#
  5# Tix Demostration Program
  6#
  7# This sample program is structured in such a way so that it can be
  8# executed from the Tix demo program "tixwidget": it must have a
  9# procedure called "RunSample". It should also have the "if" statment
 10# at the end of this file so that it can be run as a standalone
 11# program using tixwish.
 12
 13# This file demonstrates how to use multiple columns and multiple styles
 14# in the tixHList widget
 15#
 16# In a tixHList widget, you can have one ore more columns.
 17#
 18
 19import Tix
 20
 21TCL_ALL_EVENTS          = 0
 22
 23def RunSample (root):
 24    shlist = DemoSHList(root)
 25    shlist.mainloop()
 26    shlist.destroy()
 27
 28class DemoSHList:
 29    def __init__(self, w):
 30        self.root = w
 31        self.exit = -1
 32
 33        z = w.winfo_toplevel()
 34        z.wm_protocol("WM_DELETE_WINDOW", lambda self=self: self.quitcmd())
 35
 36        # We create the frame and the ScrolledHList widget
 37        # at the top of the dialog box
 38        #
 39        top = Tix.Frame( w, relief=Tix.RAISED, bd=1)
 40
 41        # Put a simple hierachy into the HList (two levels). Use colors and
 42        # separator widgets (frames) to make the list look fancy
 43        #
 44        top.a = Tix.ScrolledHList(top, options='hlist.columns 3 hlist.header 1' )
 45        top.a.pack( expand=1, fill=Tix.BOTH, padx=10, pady=10, side=Tix.TOP)
 46
 47        hlist=top.a.hlist
 48
 49        # Create the title for the HList widget
 50        #       >> Notice that we have set the hlist.header subwidget option to true
 51        #      so that the header is displayed
 52        #
 53
 54        boldfont=hlist.tk.call('tix','option','get','bold_font')
 55
 56        # First some styles for the headers
 57        style={}
 58        style['header'] = Tix.DisplayStyle(Tix.TEXT, refwindow=hlist,
 59            anchor=Tix.CENTER, padx=8, pady=2, font = boldfont )
 60
 61        hlist.header_create(0, itemtype=Tix.TEXT, text='Name',
 62            style=style['header'])
 63        hlist.header_create(1, itemtype=Tix.TEXT, text='Position',
 64            style=style['header'])
 65
 66        # Notice that we use 3 columns in the hlist widget. This way when the user
 67        # expands the windows wide, the right side of the header doesn't look
 68        # chopped off. The following line ensures that the 3 column header is
 69        # not shown unless the hlist window is wider than its contents.
 70        #
 71        hlist.column_width(2,0)
 72
 73        # This is our little relational database
 74        #
 75        boss = ('doe', 'John Doe',      'Director')
 76
 77        managers = [
 78            ('jeff',  'Jeff Waxman',    'Manager'),
 79            ('john',  'John Lee',               'Manager'),
 80            ('peter', 'Peter Kenson',   'Manager')
 81        ]
 82
 83        employees = [
 84            ('alex',  'john',   'Alex Kellman',         'Clerk'),
 85            ('alan',  'john',       'Alan Adams',               'Clerk'),
 86            ('andy',  'peter',      'Andreas Crawford', 'Salesman'),
 87            ('doug',  'jeff',       'Douglas Bloom',    'Clerk'),
 88            ('jon',   'peter',      'Jon Baraki',               'Salesman'),
 89            ('chris', 'jeff',       'Chris Geoffrey',   'Clerk'),
 90            ('chuck', 'jeff',       'Chuck McLean',             'Cleaner')
 91        ]
 92
 93        style['mgr_name'] = Tix.DisplayStyle(Tix.TEXT, refwindow=hlist)
 94
 95        style['mgr_posn'] = Tix.DisplayStyle(Tix.TEXT, padx=8, refwindow=hlist)
 96
 97        style['empl_name'] = Tix.DisplayStyle(Tix.TEXT, refwindow=hlist)
 98
 99        style['empl_posn'] = Tix.DisplayStyle(Tix.TEXT, padx=8, refwindow=hlist)
100
101        # Let configure the appearance of the HList subwidget
102        #
103        hlist.config(separator='.', width=25, drawbranch=0, indent=10)
104        hlist.column_width(0, chars=20)
105
106        # Create the boss
107        #
108        hlist.add ('.',           itemtype=Tix.TEXT, text=boss[1],
109            style=style['mgr_name'])
110        hlist.item_create('.', 1, itemtype=Tix.TEXT, text=boss[2],
111            style=style['mgr_posn'])
112
113        # Create the managers
114        #
115
116        for key,name,posn in managers :
117            e= '.'+ key
118            hlist.add(e, itemtype=Tix.TEXT, text=name,
119                style=style['mgr_name'])
120            hlist.item_create(e, 1, itemtype=Tix.TEXT, text=posn,
121                style=style['mgr_posn'])
122
123
124        for key,mgr,name,posn in employees :
125            # "." is the separator character we chose above
126
127            entrypath = '.' + mgr        + '.' + key
128
129            #           ^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^
130            #       parent entryPath / child's name
131
132            hlist.add(entrypath, text=name, style=style['empl_name'])
133            hlist.item_create(entrypath, 1, itemtype=Tix.TEXT,
134                text = posn, style = style['empl_posn'] )
135
136
137        # Use a ButtonBox to hold the buttons.
138        #
139        box= Tix.ButtonBox(top, orientation=Tix.HORIZONTAL )
140        box.add( 'ok',  text='Ok', underline=0,  width=6,
141            command = self.okcmd )
142
143        box.add( 'cancel', text='Cancel', underline=0, width=6,
144            command = self.quitcmd )
145
146        box.pack( side=Tix.BOTTOM, fill=Tix.X)
147        top.pack( side=Tix.TOP,    fill=Tix.BOTH, expand=1 )
148
149    def okcmd (self):
150        self.quitcmd()
151
152    def quitcmd (self):
153        self.exit = 0
154
155    def mainloop(self):
156        while self.exit < 0:
157            self.root.tk.dooneevent(TCL_ALL_EVENTS)
158
159    def destroy (self):
160        self.root.destroy()
161
162
163# This "if" statement makes it possible to run this script file inside or
164# outside of the main demo program "tixwidgets.py".
165#
166if __name__== '__main__' :
167    root=Tix.Tk()
168    RunSample(root)