Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Browser made. Almost done. Only things to solve are the link that is …

…clicked and the image not being displayed
  • Loading branch information...
commit f42cfae09f705a0cc5a5d545f33b1dbbd7149155 1 parent 2630c57
Vaibhav Agarwal authored
Showing with 126 additions and 0 deletions.
  1. +89 −0 sweb.py
  2. +37 −0 tkHyperlink.py
View
89 sweb.py
@@ -0,0 +1,89 @@
+from bs4 import BeautifulSoup , Tag
+import sys
+from Tkinter import *
+import tkHyperlink
+
+
+class BrowserWindow:
+
+
+ def __init__(self):
+ self.root = Tk()
+ self.root.geometry('%sx%s+0+0'% self.root.maxsize() )
+ # TODO : Root menu has to be defined here using some kind of function
+ # TODO : Functionality to add tabs whenever new page is opened
+ self.w = Text ( self.root , bg="white" , wrap = WORD )
+ self.w.grid()
+ self.w.pack(side = LEFT , fill = BOTH , expand = YES )
+ self.root.title('SiMple Web')
+ self.count = 0
+
+ def openPage (self , file):
+ input = open(file , 'r')
+ doc = input.readlines()
+ soup = BeautifulSoup(''.join(doc))
+ soup.prettify()
+ self.root.title = soup.page_title.contents[0]
+ tag = soup.page_body
+ self.parseDisplay( tag , tag.attrs )
+
+ def parseDisplay ( self , tag , style ):
+ #print "-------------------- PARSE DISPLAY LOG ----------------------"
+ #print tag
+ #print style
+ #print "---------------------Parse display log end -------------------"
+ for child in tag.children:
+ if isinstance( child , Tag ):
+ if child.name == 'link' :
+ self.writecontent( child['display'] , child['markref'] , 0 )
+ elif child.name == 'image':
+ self.writecontent( child['markref'] , style , -1 )
+ elif child.attrs:
+ self.parseDisplay(child , child.attrs)
+ else:
+ self.parseDisplay(child , style)
+ else:
+ self.count = self.count+1
+ self.writecontent(child , style , self.count)
+
+ def writecontent(self , msg , style , tagCreationCount ):
+ self.w['state']='normal'
+ print "-----------------------TKINTER LOG------------------------"
+ print msg
+ print style
+ print "------------------------Tkinter Log Done -----------------"
+ if tagCreationCount == 0:
+ hyperlink = tkHyperlink.HyperlinkManager(self.w)
+ self.w.insert('end' , msg , hyperlink.add(lambda: self.reallinkClick(style)))
+ return
+ elif tagCreationCount == -1:
+ print "Image found. Displaying !!"
+ img = PhotoImage ( file = 'Sachin_Ramesh_Tendulkar.gif' )
+ self.w.image_create ( 'end' , image = img )
+ print "Image created"
+ return
+ if style:
+ try:
+ ucolor = style['color']
+ except KeyError:
+ ucolor = "purple"
+ self.w.tag_configure( tagCreationCount , foreground = ucolor )
+ self.w.insert('end' , msg , ( tagCreationCount ) )
+ else:
+ self.w.insert('end' , msg , ( 'default' ) )
+ self.w['state']='disabled'
+
+
+ def linkClick(event):
+ print "Link Clicked"
+
+ def start(self):
+ self.root.mainloop()
+
+if __name__=="__main__":
+ window = BrowserWindow()
+ window.openPage(sys.argv[1])
+ window.start()
+
+
+
View
37 tkHyperlink.py
@@ -0,0 +1,37 @@
+from Tkinter import *
+
+class HyperlinkManager:
+
+ def __init__(self, text):
+
+ self.text = text
+
+ self.text.tag_config("hyper", foreground="blue", underline=1)
+
+ self.text.tag_bind("hyper", "<Enter>", self._enter)
+ self.text.tag_bind("hyper", "<Leave>", self._leave)
+ self.text.tag_bind("hyper", "<Button-1>", self._click)
+
+ self.reset()
+
+ def reset(self):
+ self.links = {}
+
+ def add(self, action):
+ # add an action to the manager. returns tags to use in
+ # associated text widget
+ tag = "hyper-%d" % len(self.links)
+ self.links[tag] = action
+ return "hyper", tag
+
+ def _enter(self, event):
+ self.text.config(cursor="hand2")
+
+ def _leave(self, event):
+ self.text.config(cursor="")
+
+ def _click(self, event):
+ for tag in self.text.tag_names(CURRENT):
+ if tag[:6] == "hyper-":
+ self.links[tag]()
+ return
Please sign in to comment.
Something went wrong with that request. Please try again.