Advertisement
here2share

# Tk_listboxfullscroll.py

Sep 24th, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. # Tk_listboxfullscroll.py
  2.  
  3. from Tkinter import *
  4.  
  5. def yview(*args):
  6.     """ scroll both listboxes together """
  7.     if len(args) is 1: args = "scroll", args, "units"
  8.     select1.yview(*args)
  9.     select2.yview(*args)
  10.  
  11. root = Tk()
  12.  
  13. keyU=38
  14. keyD=40
  15. keyL=37
  16. keyR=39
  17.  
  18. root.hasFocus = None
  19.  
  20. frame = Frame(root)
  21. frame.pack()
  22. scroll = Scrollbar(frame, orient=VERTICAL, command=yview)
  23. select1 = Listbox(frame, height=30, width=40,  selectmode=SINGLE, selectbackground='purple')
  24.     # + exportselection=False
  25. select1.config(yscrollcommand=scroll.set)
  26. select1.pack(side=LEFT)
  27. select2 = Listbox(frame, height=30, width=160, selectmode=SINGLE, selectbackground='purple') # selectmode=MULTIPLE
  28. select2.config(yscrollcommand=scroll.set)
  29. scroll.pack(side=RIGHT, fill=Y)
  30. select2.pack(side=RIGHT)
  31.  
  32. def OnFocus1(e):
  33.     root.hasFocus = 'Column A'
  34.     OnSelect(e)
  35.  
  36. def OnFocus2(e):
  37.     root.hasFocus = 'Column B'
  38.     OnSelect(e)
  39.  
  40. def OnSelect(e):
  41.     w = e.widget
  42.     cbgSelect()
  43.     print root.hasFocus
  44.     for i in w.curselection():
  45.         print 'You selected: %s' % (w.get(i)) ### ZZZ generic for multi-select option
  46.         select1.itemconfigure(i, fg='white', bg='purple')
  47.         select2.itemconfigure(i, fg='white', bg='purple')
  48.     print
  49.     frame.focus_set()
  50.  
  51. def OnMouseWheel(event):
  52.     e = -25
  53.     if event.delta < 1: e = 25
  54.     yview(e)
  55.     return 'break'
  56.  
  57. def OnKeyPressed(event):
  58.     e=0
  59.     if   event.keycode is keyU: e = -1
  60.     elif event.keycode is keyD: e = 1
  61.     yview(e)
  62.     return 'break'
  63.  
  64. select1.bind('<FocusIn>', OnFocus1)
  65. select2.bind('<FocusIn>', OnFocus2)
  66. frame.bind_all('<MouseWheel>',  OnMouseWheel)
  67. #   frame.bind_all("<Button-1>", OnMouseL) ### replaced with OnFocus
  68. frame.bind_all('<KeyPress>',    OnKeyPressed)
  69. #   frame.bind_all('<<ListboxSelect>>', OnSelect)
  70.  
  71. def setSelect() :
  72.     select1.delete(0,END)
  73.     select2.delete(0,END)
  74.     for i in range(100):
  75.         select1.insert('end','%s' % i)
  76.         select2.insert('end','%s' % i)
  77.     cbgSelect()
  78.        
  79. def cbgSelect() :
  80.     for i in range(0, 100):
  81.         cbg = '#FFFFFF'
  82.         if i % 2: cbg = '#CCCCFF'
  83.         select1.itemconfigure(i, fg='#000000', bg=cbg)
  84.         select2.itemconfigure(i, fg='#000000', bg=cbg)
  85.        
  86. setSelect()
  87.  
  88. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement