Advertisement
here2share

# Tk_text_based_image_browser_template.py

Jul 27th, 2015
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.86 KB | None | 0 0
  1. # Tk_text_based_image_browser_template.py
  2.  
  3. try:
  4.     # Python2
  5.     from Tkinter import *
  6.     from urllib2 import urlopen
  7. except ImportError:
  8.     # Python3
  9.     from tkinter import *
  10.     from urllib.request import urlopen
  11. import webbrowser
  12. import time
  13.  
  14. root=Tk()
  15. root.geometry('1280x654+0+0')
  16. root.title('Online Image Browser')
  17.  
  18. topFrame=LabelFrame(root,text="",height=600)
  19. topFrame.pack(fill=X)
  20. topFrame.pack_propagate(False)
  21.  
  22. Frame2=LabelFrame(root,height=24,borderwidth=0)
  23. Frame2.pack(fill=X)
  24. Frame2.pack_propagate(False)
  25.  
  26. Frame3=LabelFrame(root,height=30,borderwidth=0)
  27. Frame3.pack(fill=X)
  28. Frame3.pack_propagate(False)
  29.  
  30. ffff=Listbox(topFrame,selectmode=EXTENDED) # Bingo! Multiple links can be chosen, using the Shift and Control keyboard modifiers
  31. ffff.pack(fill=BOTH,expand=YES,side=LEFT)
  32.  
  33. scroll=Scrollbar(topFrame)
  34. scroll.pack(side=RIGHT,fill=Y,expand=NO)
  35.  
  36. scroll.configure(command=ffff.yview)
  37. ffff.configure(yscrollcommand=scroll.set)
  38.  
  39. top = Toplevel()
  40.  
  41. curr=[]
  42. links=[]
  43. jpegs=[]
  44. keyurl=''
  45. back=[]
  46.  
  47. def dblclk(event):
  48.     top.deiconify()
  49.     html=ffff.curselection()
  50.     html=curr[int(html[0])]
  51.     root.update()
  52.     if html.lower().endswith(('.jpg','.jpeg',)):
  53.         fullsize(html)
  54.     else:
  55.         dig_urls(html)
  56. #
  57. def update_list(items):
  58.     global curr
  59.     ffff.delete(0, END)
  60.     for item in items:
  61.         ffff.insert(END,item)
  62.     curr=items
  63.     top.withdraw()
  64.     root.update()
  65. update_list(curr) # None
  66. #
  67. def from_clipboard():
  68.     data = root.clipboard_get()
  69.     url.set(data)
  70.     top.deiconify()
  71.     root.update()
  72.     url_images()
  73. #
  74. top = Toplevel()
  75. top.transient(root)
  76. top.geometry('300x60+480+200')
  77. top.title('*')
  78. msg = Label(top, text="\nProcessing, Please Wait...")
  79. msg.pack()
  80. top.withdraw()
  81. #
  82. def view_selected():
  83.     vsel=ffff.curselection()
  84.     if len(vsel) == 1:
  85.         url.set(curr[int(vsel[0])])
  86.     for i in vsel:
  87.         i=int(i)
  88.         webbrowser.open(curr[i])
  89.         time.sleep(0.5)
  90. #
  91. def all_jpgs(in_page):
  92.     global jpegs
  93.     jpegs=[]
  94.     home=keyurl
  95.     home='/'.join(home.split('/')[:3])
  96.     images=re.findall('''["']((?i)\S+\.jpe?g)''', in_page) #
  97.     for i in images:
  98.         print i,'+++++'
  99.         if '//' in i:
  100.             jpegs.append(i)
  101.         else:
  102.             tmp=(keyurl+'/'+i).replace('//','/')
  103.             jpegs.append(tmp.replace(':',':/'))
  104.             tmp=(home+'/'+i).replace('//','/')
  105.             jpegs.append(tmp.replace(':',':/'))
  106.     jpegs=sorted(list(set(jpegs)))
  107.     update_list(jpegs)
  108.     ffff.configure(selectmode=EXTENDED); print 'Multi-Select Mode'
  109.     ffff.unbind("<Double-Button-1>")
  110.     web.configure(state=NORMAL)
  111. #
  112. def all_links(in_page):
  113.     global links
  114.     links=[]
  115.     external=[]
  116.     home=keyurl
  117.     if '//' in home:
  118.         home='/'.join(home.split('/')[:3])
  119.     for tmp in re.findall('''[HREF|href]=["'](.[^"']+)[/"']''', in_page):
  120.         if tmp.startswith(tuple('# java mailto'.split())):
  121.             next
  122.         if tmp[0] == '/':
  123.             tmp=home+tmp
  124.             tmp=tmp.replace('//','/').replace(':',':/')
  125.             if tmp not in external+links:
  126.                 links.append(tmp)
  127.         else:
  128.             if tmp not in external+links and '://' in tmp:
  129.                 external.append(tmp)
  130.     links=external+links
  131.     links=sorted(links)
  132. #
  133. def fullsize(fsz):
  134.     global jlist
  135.     jlist=[]
  136.     if '#' not in fsz:
  137.         if '-' in fsz[-7:]:
  138.             fsz=fsz.split('-')
  139.             fsz[-2]=fsz[-2]+'-#.jpg'
  140.             fsz='-'.join(fsz[:-1])
  141.         else:
  142.             fsz=fsz.split('.')
  143.             fsz[-2]=fsz[-2][:-2]+'#'
  144.             fsz='.'.join(fsz)
  145.     for i in range(10):
  146.         tmp=fsz.replace('#',str(i))
  147.         jlist.append(tmp)
  148.     for i in range(50+1):
  149.         tmp=str(i).zfill(2)
  150.         tmp=fsz.replace('#',tmp)
  151.         jlist.append(tmp)
  152.     update_list(jlist)
  153. #
  154. def dig_urls(html):
  155.     global keyurl
  156.     html=urlopen(html)
  157.     keyurl=html.geturl()
  158.     if keyurl not in back: back.append(keyurl)
  159.     url.set(keyurl)
  160.     in_page=html.read()
  161.     all_links(in_page)
  162.     all_jpgs(in_page)
  163. #
  164. def url_images():
  165.     web.configure(state=NORMAL)
  166.     ffff.configure(selectmode=EXTENDED); print 'Multi-Select Mode'
  167.     ffff.unbind("<Double-Button-1>")
  168.     html=ent.get()
  169.     if html.lower().endswith(('.jpg','.jpeg',)):
  170.         fullsize(html)
  171.     else:
  172.         top.deiconify()
  173.         root.update()
  174.         if html <> keyurl:
  175.             dig_urls(html)
  176.         else:
  177.             update_list(jpegs)
  178. #
  179. def url_links():
  180.     web.configure(state=DISABLED)
  181.     ffff.configure(selectmode=SINGLE); print 'Single-Select Mode'
  182.     ffff.bind("<Double-Button-1>", dblclk)
  183.     update_list(links)
  184. #
  185. def history():
  186.     global curr
  187.     ffff.configure(selectmode=SINGLE); print 'Single-Select Mode'
  188.     ffff.bind("<Double-Button-1>", dblclk)
  189.     curr=back
  190.     update_list(back)
  191.  
  192. btn=Button(Frame2,text="Enter URL",width=16,command=url_images)
  193. btn.pack(side=LEFT)
  194.  
  195. url=StringVar()
  196. ent=Entry(Frame2,width="160",textvariable=url)
  197. ent.pack(side=LEFT)
  198.  
  199. web=Button(Frame3,text="Web View",width=16,command=view_selected)
  200. web.pack(side=LEFT)
  201.  
  202. btn=Button(Frame3,text="View Links",width=16,command=url_links)
  203. btn.pack(side=LEFT)
  204.  
  205. cpb=Button(Frame3,text="Clipboard",width=16,command=from_clipboard)
  206. cpb.pack(side=LEFT)
  207.  
  208. lbl=Label(Frame3)
  209. lbl.pack(side=LEFT)
  210.  
  211. btn=Button(Frame3,text="History",width=16,command=history)
  212. btn.pack(side=RIGHT)
  213.  
  214. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement