here2share

# Tk_multi_clipboard.py

Mar 26th, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. # Tk_multi_clipboard.py ZZZ tabspaces oddly disappears from listbox
  2.  
  3. import os
  4. import time
  5. import string
  6. import subprocess
  7. from Tkinter import *
  8. root=Tk()
  9. root.title('Multi-Clipboard')
  10.  
  11. class Cv: 0
  12. cv=Cv()
  13.  
  14. def yview(*args):
  15.     if len(args) is 1:
  16.         args="scroll", args, "units"
  17.     select1.yview(*args)
  18.     multi.yview(*args)
  19. 0
  20. def OnMouseWheel(event): # ZZZ ?
  21.     d=-0.2
  22.     if event.delta < 1:
  23.         d=0.2
  24.     # to sync listboxes
  25.     d+=cv.yDuoScroll
  26.     cv.yDuoScroll=min(1,max(0,d))
  27.     select1.yview('moveto', str(d))
  28.     multi.yview('moveto', str(d))
  29.     return 'break'
  30. 0
  31. data=['' for z in range(100)]
  32. def add_row(): # ZZZ Save To Primary
  33.     if len(data) < 99:
  34.         data.insert(20, select2.get(1.0, END))
  35.         renew_listbox()
  36.     else:
  37.         print "Copy Not Saved To Primary. List Is Full"
  38. 0
  39. def delete_row(): # ZZZ
  40.     pass
  41. 0
  42. def edit_pro():
  43.     if cv.file.closed:
  44.         root.attributes('-disabled',0)
  45.         return
  46.     print '???'
  47.     root.after(3000, edit_pro)
  48. 0
  49. def edit(): # ZZZ
  50.     root.attributes('-disabled',1)
  51.     for z in root.attributes(): print z
  52.     try:
  53.         sss=files[data[cv.ddd]]
  54.     except:
  55.         sss=dir+'\\'+str(time.clock())[4:-4]+'__temp.txt'
  56.     file=open(sss, 'w')
  57.     file.write(data[cv.ddd])
  58.     file.close()
  59.     subprocess.Popen(['notepad++.exe', sss])
  60.     cv.file = open(sss)
  61.     edit_pro()
  62. 0
  63. topframe=Frame(root)
  64. topframe.grid()
  65.  
  66. b3=Button(topframe , text='Main Text Editor', bg='yellow', command=edit)
  67. b3.grid(column=0, row=0)
  68.  
  69. Label(topframe , text=' '*4).grid(column=1, row=0)
  70.  
  71. b1=Button(topframe , text='Add Row', bg='yellow', command=add_row)
  72. b1.grid(column=2, row=0)
  73.  
  74. b2=Button(topframe , text='Delete Selected', bg='yellow', command=delete_row)
  75. b2.grid(column=3, row=0)
  76.  
  77. frame=Frame(root)
  78. frame.grid()
  79. scroll=Scrollbar(frame, orient=VERTICAL, command=yview)
  80.  
  81. select1=Listbox(frame, height=30, width=40,  selectmode=SINGLE, selectbackground='purple') # ZZZ selectmode=MULTIPLE
  82.     # + exportselection=False
  83. select1.config(yscrollcommand=scroll.set)
  84. select1.grid(column=1, row=1, sticky='ns')
  85. multi=Listbox(frame, height=30, width=2,  selectmode=MULTIPLE, selectbackground='red')
  86. multi.config(yscrollcommand=scroll.set)
  87. multi.grid(column=0, row=1, sticky='ns')
  88. for z in range(100):
  89.         multi.insert(END,'')
  90.         cbg='white'
  91.         if z % 2: cbg='#CCCCFF'
  92.         multi.itemconfigure(z, fg='black', bg=cbg)
  93. 0
  94. scroll.grid(column=2, row=1, sticky='ns')
  95.  
  96. label=Label(root)
  97. label.grid(column=3, row=1)
  98. select2=Text(label, height=30, width=120)
  99. scroll2=Scrollbar(root, orient=VERTICAL, command=yview)
  100. select2.config(yscrollcommand=scroll2.set)
  101. select2.grid(sticky='news')
  102. scroll2.grid(column=4, row=1, sticky='ns')
  103.  
  104. cv.ddd=0
  105. cv.prev=''
  106. cv.tab=0
  107. cv.yDuoScroll=0
  108. cv.active='textbox'
  109. files={}
  110.  
  111. dir=os.path.join(os.path.expanduser('~'))+'\\Documents\\MultiClip'
  112. print dir
  113. def select_data(): # ZZZ
  114.     global data
  115.     if not os.path.exists(dir):
  116.         print "Path: " , dir ,  " Not Found"
  117.         ans=raw_input("Create That Directory? (y/n) ")
  118.         if 'y' in ans.lower():
  119.             try:
  120.                 os.mkdir(dir)
  121.                 print "Directory ", dir ," Created "
  122.             except:
  123.                 print "Unable To Create Directory ", dir
  124.                 return
  125.  
  126.     byDate=[]
  127.     for z,f in enumerate(os.listdir(dir)[:80]):
  128.         f=dir+'\\'+f
  129.         byDate.append([os.path.getmtime(f),f])
  130.     byDate.sort(reverse=True)
  131.     for z,f in enumerate(byDate):
  132.         file=open(f[-1], 'r')
  133.         s=file.read()
  134.         data[z+20]=s
  135.         files.update({s: f[-1]})
  136. 0
  137. def renew_listbox():
  138.     select1.delete(0, END)
  139.     for z,content in enumerate(data):
  140.         if not content.strip():
  141.             content=''
  142.         n=str(z+1).zfill(3)+':'
  143.         if z < 20:
  144.             n+=u" \u25BC" # '\u25B2' is upward
  145.         select1.insert(END,'%s %s' % (n, content.lstrip()))
  146.         cbg='white'
  147.         if z % 2: cbg='#CCCCFF'
  148.         select1.itemconfigure(z, fg='black', bg=cbg)
  149.     select1.itemconfigure(cv.ddd, fg='white', bg='purple')
  150.     select1.activate(cv.ddd)
  151. 0
  152. def diff(x,y): # because ACTIVE and ANCHOR failed in multiple mode
  153.     z=max([tuple(set(x).difference(set(y))) for x,y in [(x,y),(y,x)]])
  154.     if z:
  155.         return z[-1]
  156.     return cv.ddd
  157. 0
  158. def OnSelect(e):
  159.     z=cv.ddd
  160.     w=e.widget
  161.     try: cv.ddd=w.curselection()[-1]
  162.     except: 0
  163.     n=cv.ddd
  164.     cbg='white'
  165.     if z % 2: cbg='#CCCCFF'
  166.     select1.itemconfigure(z, fg='black', bg=cbg)
  167.     sss=data[n]
  168.     setClipboardData()
  169.     select2.delete(1.0, END)
  170.     select2.insert(END, sss)
  171. 0
  172. def OnChange(e):
  173.     n=cv.ddd
  174.     sss=data[n]
  175.     s=e.char
  176.     try:
  177.         c=ord(s)
  178.     except:
  179.         return
  180.     if c == 8: sss=sss[:-1]
  181.     elif c == 13: sss+='\n'
  182.     elif c == 9: sss+='\t' # ZZZ ?????
  183.     else: sss+=s
  184.     #sss=''.join(s for s in sss if s in string.printable)
  185.     try:
  186.         files[sss]=files.pop(data[n])
  187.     except:
  188.         0
  189.     data[n]=sss
  190.     OnSelect(e)
  191.     renew_listbox()
  192. 0
  193. def getClipboardData():
  194.     try:
  195.         z=root.clipboard_get()
  196.         return z
  197.     except:
  198.         return ''
  199. 0
  200. def setClipboardData():
  201.     root.clipboard_clear()
  202.     root.clipboard_append(data[cv.ddd])
  203. 0
  204. def loop_check():
  205.     sss=getClipboardData()
  206.     if sss:
  207.         if sss not in data: # ZZZ
  208.             data.insert(0,sss)
  209.             data.pop(20)
  210.             select2.delete(1.0, END)
  211.             select2.insert(END, sss)
  212.             cv.ddd=0
  213.             renew_listbox()
  214.     root.after(5000, loop_check)
  215. 0
  216. select1.bind('<<ListboxSelect>>', OnSelect)
  217. root.bind('<Key>', OnChange)
  218. scroll.bind_all('<MouseWheel>',  OnMouseWheel)
  219.  
  220. select_data()
  221. renew_listbox()
  222. loop_check()
  223. root.mainloop()
Add Comment
Please, Sign In to add comment