/Demo/tix/samples/SHList2.py

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