daily pastebin goal
85%
SHARE
TWEET

MultiListbox Original

bluegator4 May 3rd, 2014 164 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #guilistview.py
  2. from tkinter import *
  3. class MultiListbox(Frame):
  4.     '''MultiListbox made by Labels as table header and Listbox as table colomns'''
  5.     def __init__(self, master, lists):
  6.         Frame.__init__(self, master)
  7.         self.lists = []
  8.         for l,w in lists:
  9.             frame = Frame(self); frame.pack(side=LEFT, expand=YES, fill=BOTH)
  10.             Label(frame, text=l, borderwidth=1, relief=RAISED).pack(fill=X)
  11.             lb = Listbox(frame, width=w, borderwidth=0, selectborderwidth=0,
  12.                  relief=FLAT, exportselection=FALSE)
  13.             lb.pack(expand=YES, fill=BOTH)
  14.             self.lists.append(lb)
  15.             lb.bind('<B1-Motion>', lambda e, s=self: s.Select(e.y))
  16.             lb.bind('<Button-1>', lambda e, s=self: s.Select(e.y))
  17.             lb.bind('<Leave>', lambda e: 'break')
  18.             lb.bind('<B2-Motion>', lambda e, s=self: s._b2motion(e.x, e.y))
  19.             lb.bind('<Button-2>', lambda e, s=self: s._button2(e.x, e.y))
  20.         frame = Frame(self); frame.pack(side=LEFT, fill=Y)
  21.         Label(frame, borderwidth=1, relief=RAISED).pack(fill=X)
  22.         sb = Scrollbar(frame, orient=VERTICAL, command=self._scroll)
  23.         sb.pack(expand=YES, fill=Y)
  24.         self.lists[0]['yscrollcommand']=sb.set
  25.  
  26.     def _select(self, y):
  27.         row = self.lists[0].nearest(y)
  28.         self.selection_clear(0, END)
  29.         self.selection_set(row)
  30.         return 'break'
  31.  
  32.     def _button2(self, x, y):
  33.         for l in self.lists: l.scan_mark(x, y)
  34.         return 'break'
  35.  
  36.     def _b2motion(self, x, y):
  37.         for l in self.lists: l.scan_dragto(x, y)
  38.         return 'break'
  39.  
  40.     def _scroll(self, *args):
  41.         for l in self.lists:
  42.             l.yview(*args)
  43.  
  44.     def curselection(self):
  45.         return self.lists[0].curselection()
  46.  
  47.     def delete(self, first, last=None):
  48.         for l in self.lists:
  49.             l.delete(first, last)
  50.  
  51.     def get(self, first, last=None):
  52.         result = []
  53.         for l in self.lists:
  54.             result.append(l.get(first,last))
  55.         if last: return list(map(*[None] + result))
  56.         return result
  57.        
  58.     def index(self, index):
  59.         self.lists[0].index(index)
  60.  
  61.     def insert(self, index, *elements):
  62.         for e in elements:
  63.             i = 0
  64.             for l in self.lists:
  65.                 l.insert(index, e[i])
  66.                 i = i + 1
  67.  
  68.     def size(self):
  69.         return self.lists[0].size()
  70.  
  71.     def see(self, index):
  72.         for l in self.lists:
  73.             l.see(index)
  74.  
  75.     def selection_anchor(self, index):
  76.         for l in self.lists:
  77.             l.SelectionAnchor(index)
  78.  
  79.     def selection_clear(self, first, last=None):
  80.         for l in self.lists:
  81.             l.SelectionClear(first, last)
  82.  
  83.     def selection_includes(self, index):
  84.         return self.lists[0].SelectionIncludes(index)
  85.  
  86.     def selection_set(self, first, last=None):
  87.         self.item_selected=[first,]+self.get(first)
  88.         for l in self.lists:
  89.             l.selection_set(first, last)
  90.     def not_focus(self):#suhail
  91.         for l in self.lists:
  92.             l['takefocus']=False
RAW Paste Data
Top