Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_text_based_image_browser_template.py
- try:
- # Python2
- from Tkinter import *
- from urllib2 import urlopen
- except ImportError:
- # Python3
- from tkinter import *
- from urllib.request import urlopen
- import webbrowser
- import time
- root=Tk()
- root.geometry('1280x654+0+0')
- root.title('Online Image Browser')
- topFrame=LabelFrame(root,text="",height=600)
- topFrame.pack(fill=X)
- topFrame.pack_propagate(False)
- Frame2=LabelFrame(root,height=24,borderwidth=0)
- Frame2.pack(fill=X)
- Frame2.pack_propagate(False)
- Frame3=LabelFrame(root,height=30,borderwidth=0)
- Frame3.pack(fill=X)
- Frame3.pack_propagate(False)
- ffff=Listbox(topFrame,selectmode=EXTENDED) # Bingo! Multiple links can be chosen, using the Shift and Control keyboard modifiers
- ffff.pack(fill=BOTH,expand=YES,side=LEFT)
- scroll=Scrollbar(topFrame)
- scroll.pack(side=RIGHT,fill=Y,expand=NO)
- scroll.configure(command=ffff.yview)
- ffff.configure(yscrollcommand=scroll.set)
- top = Toplevel()
- curr=[]
- links=[]
- jpegs=[]
- keyurl=''
- back=[]
- def dblclk(event):
- top.deiconify()
- html=ffff.curselection()
- html=curr[int(html[0])]
- root.update()
- if html.lower().endswith(('.jpg','.jpeg',)):
- fullsize(html)
- else:
- dig_urls(html)
- #
- def update_list(items):
- global curr
- ffff.delete(0, END)
- for item in items:
- ffff.insert(END,item)
- curr=items
- top.withdraw()
- root.update()
- update_list(curr) # None
- #
- def from_clipboard():
- data = root.clipboard_get()
- url.set(data)
- top.deiconify()
- root.update()
- url_images()
- #
- top = Toplevel()
- top.transient(root)
- top.geometry('300x60+480+200')
- top.title('*')
- msg = Label(top, text="\nProcessing, Please Wait...")
- msg.pack()
- top.withdraw()
- #
- def view_selected():
- vsel=ffff.curselection()
- if len(vsel) == 1:
- url.set(curr[int(vsel[0])])
- for i in vsel:
- i=int(i)
- webbrowser.open(curr[i])
- time.sleep(0.5)
- #
- def all_jpgs(in_page):
- global jpegs
- jpegs=[]
- home=keyurl
- home='/'.join(home.split('/')[:3])
- images=re.findall('''["']((?i)\S+\.jpe?g)''', in_page) #
- for i in images:
- print i,'+++++'
- if '//' in i:
- jpegs.append(i)
- else:
- tmp=(keyurl+'/'+i).replace('//','/')
- jpegs.append(tmp.replace(':',':/'))
- tmp=(home+'/'+i).replace('//','/')
- jpegs.append(tmp.replace(':',':/'))
- jpegs=sorted(list(set(jpegs)))
- update_list(jpegs)
- ffff.configure(selectmode=EXTENDED); print 'Multi-Select Mode'
- ffff.unbind("<Double-Button-1>")
- web.configure(state=NORMAL)
- #
- def all_links(in_page):
- global links
- links=[]
- external=[]
- home=keyurl
- if '//' in home:
- home='/'.join(home.split('/')[:3])
- for tmp in re.findall('''[HREF|href]=["'](.[^"']+)[/"']''', in_page):
- if tmp.startswith(tuple('# java mailto'.split())):
- next
- if tmp[0] == '/':
- tmp=home+tmp
- tmp=tmp.replace('//','/').replace(':',':/')
- if tmp not in external+links:
- links.append(tmp)
- else:
- if tmp not in external+links and '://' in tmp:
- external.append(tmp)
- links=external+links
- links=sorted(links)
- #
- def fullsize(fsz):
- global jlist
- jlist=[]
- if '#' not in fsz:
- if '-' in fsz[-7:]:
- fsz=fsz.split('-')
- fsz[-2]=fsz[-2]+'-#.jpg'
- fsz='-'.join(fsz[:-1])
- else:
- fsz=fsz.split('.')
- fsz[-2]=fsz[-2][:-2]+'#'
- fsz='.'.join(fsz)
- for i in range(10):
- tmp=fsz.replace('#',str(i))
- jlist.append(tmp)
- for i in range(50+1):
- tmp=str(i).zfill(2)
- tmp=fsz.replace('#',tmp)
- jlist.append(tmp)
- update_list(jlist)
- #
- def dig_urls(html):
- global keyurl
- html=urlopen(html)
- keyurl=html.geturl()
- if keyurl not in back: back.append(keyurl)
- url.set(keyurl)
- in_page=html.read()
- all_links(in_page)
- all_jpgs(in_page)
- #
- def url_images():
- web.configure(state=NORMAL)
- ffff.configure(selectmode=EXTENDED); print 'Multi-Select Mode'
- ffff.unbind("<Double-Button-1>")
- html=ent.get()
- if html.lower().endswith(('.jpg','.jpeg',)):
- fullsize(html)
- else:
- top.deiconify()
- root.update()
- if html <> keyurl:
- dig_urls(html)
- else:
- update_list(jpegs)
- #
- def url_links():
- web.configure(state=DISABLED)
- ffff.configure(selectmode=SINGLE); print 'Single-Select Mode'
- ffff.bind("<Double-Button-1>", dblclk)
- update_list(links)
- #
- def history():
- global curr
- ffff.configure(selectmode=SINGLE); print 'Single-Select Mode'
- ffff.bind("<Double-Button-1>", dblclk)
- curr=back
- update_list(back)
- btn=Button(Frame2,text="Enter URL",width=16,command=url_images)
- btn.pack(side=LEFT)
- url=StringVar()
- ent=Entry(Frame2,width="160",textvariable=url)
- ent.pack(side=LEFT)
- web=Button(Frame3,text="Web View",width=16,command=view_selected)
- web.pack(side=LEFT)
- btn=Button(Frame3,text="View Links",width=16,command=url_links)
- btn.pack(side=LEFT)
- cpb=Button(Frame3,text="Clipboard",width=16,command=from_clipboard)
- cpb.pack(side=LEFT)
- lbl=Label(Frame3)
- lbl.pack(side=LEFT)
- btn=Button(Frame3,text="History",width=16,command=history)
- btn.pack(side=RIGHT)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement