home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / MULTISCROLLEDLISTS.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  3.9 KB  |  139 lines

  1. # One or more ScrolledLists with HSeparators between them.
  2. # There is a hierarchical relationship between them:
  3. # the right list displays the substructure of the selected item
  4. # in the left list.
  5.  
  6. import string
  7. from Tkinter import *
  8. from WindowList import ListedToplevel
  9. from Separator import HSeparator
  10. from ScrolledList import ScrolledList
  11.  
  12. class MultiScrolledLists:
  13.     
  14.     def __init__(self, root, nlists=2):
  15.         assert nlists >= 1
  16.         self.root = root
  17.         self.nlists = nlists
  18.         self.path = []
  19.         # create top
  20.         self.top = top = ListedToplevel(root)
  21.         top.protocol("WM_DELETE_WINDOW", self.close)
  22.         top.bind("<Escape>", self.close)
  23.         self.settitle()
  24.         # create frames and separators in between
  25.         self.frames = []
  26.         self.separators = []
  27.         last = top
  28.         for i in range(nlists-1):
  29.             sepa = HSeparator(last)
  30.             self.separators.append(sepa)
  31.             frame, last = sepa.parts()
  32.             self.frames.append(frame)
  33.         self.frames.append(last)
  34.         # create labels and lists
  35.         self.labels = []
  36.         self.lists = []
  37.         for i in range(nlists):
  38.             frame = self.frames[i]
  39.             label = Label(frame, text=self.subtitle(i),
  40.                 relief="groove", borderwidth=2)
  41.             label.pack(fill="x")
  42.             self.labels.append(label)
  43.             list = ScrolledList(frame, width=self.width(i),
  44.                 height=self.height(i))
  45.             self.lists.append(list)
  46.             list.on_select = \
  47.                 lambda index, i=i, self=self: self.on_select(index, i)
  48.             list.on_double = \
  49.                 lambda index, i=i, self=self: self.on_double(index, i)
  50.         # fill leftmost list (rest get filled on demand)
  51.         self.fill(0)
  52.         # XXX one after_idle isn't enough; two are...
  53.         top.after_idle(self.call_pack_propagate_1)
  54.     
  55.     def call_pack_propagate_1(self):
  56.         self.top.after_idle(self.call_pack_propagate)
  57.     
  58.     def call_pack_propagate(self):
  59.         for frame in self.frames:
  60.             frame.pack_propagate(0)
  61.     
  62.     def close(self, event=None):
  63.         self.top.destroy()
  64.     
  65.     def settitle(self):
  66.         short = self.shorttitle()
  67.         long = self.longtitle()
  68.         if short and long:
  69.             title = short + " - " + long
  70.         elif short:
  71.             title = short
  72.         elif long:
  73.             title = long
  74.         else:
  75.             title = "Untitled"
  76.         icon = short or long or title
  77.         self.top.wm_title(title)
  78.         self.top.wm_iconname(icon)
  79.  
  80.     def longtitle(self):
  81.         # override this
  82.         return "Multi Scrolled Lists"
  83.     
  84.     def shorttitle(self):
  85.         # override this
  86.         return None
  87.     
  88.     def width(self, i):
  89.         # override this
  90.         return 20
  91.     
  92.     def height(self, i):
  93.         # override this
  94.         return 10
  95.     
  96.     def subtitle(self, i):
  97.         # override this
  98.         return "Column %d" % i
  99.      
  100.     def fill(self, i):
  101.         for k in range(i, self.nlists):
  102.             self.lists[k].clear()
  103.             self.labels[k].configure(text=self.subtitle(k))
  104.         list = self.lists[i]
  105.         l = self.items(i)
  106.         for s in l:
  107.             list.append(s)
  108.         
  109.     def on_select(self, index, i):
  110.         item = self.lists[i].get(index)
  111.         del self.path[i:]
  112.         self.path.append(item)
  113.         if i+1 < self.nlists:
  114.             self.fill(i+1)
  115.    
  116.     def items(self, i):
  117.         # override this
  118.         l = []
  119.         for k in range(10):
  120.             s = str(k)
  121.             if i > 0:
  122.                 s = self.path[i-1] + "." + s
  123.             l.append(s)
  124.         return l
  125.     
  126.     def on_double(self, index, i):
  127.         pass
  128.  
  129.  
  130. def main():
  131.     root = Tk()
  132.     quit = Button(root, text="Exit", command=root.destroy)
  133.     quit.pack()
  134.     MultiScrolledLists(root, 4)
  135.     root.mainloop()
  136.  
  137. if __name__ == "__main__":
  138.     main()
  139.