1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | ## ## ## ################################################ ## ###### Please Don't Remove Author Name ######### ## ############# Thanks ########################### ## ################################################ ## ## __author__='''
###################################################### By ######################################################
Suraj Singh surajsinghbisht054@gmail.com http://www.bitforestinfo.com/
###################################################### '''
import __builtin__ import re import keyword from Graphics import Tkinter
def any(name, alternates): "Return a named group pattern matching list of alternates." return "(?P<%s>" % name + "|".join(alternates) + ")"
def ty(): kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b" builtinlist = [str(name) for name in dir(__builtin__) if not name.startswith('_')] builtinlist.remove('print') builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b" comment = any("COMMENT", [r"#[^\n]*"]) stringprefix = r"(\br|u|ur|R|U|UR|Ur|uR|b|B|br|Br|bR|BR)?" sqstring = stringprefix + r"'[^'\\\n]*(\\.[^'\\\n]*)*'?" dqstring = stringprefix + r'"[^"\\\n]*(\\.[^"\\\n]*)*"?' sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?" dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?' string = any("STRING", [sq3string, dq3string, sqstring, dqstring]) return kw + "|" + builtin + "|" + comment + "|" + string +\ "|" + any("SYNC", [r"\n"])
def _coordinate(start,end,string): srow=string[:start].count('\n')+1 # starting row scolsplitlines=string[:start].split('\n') if len(scolsplitlines)!=0: scolsplitlines=scolsplitlines[len(scolsplitlines)-1] scol=len(scolsplitlines)# Ending Column lrow=string[:end+1].count('\n')+1 lcolsplitlines=string[:end].split('\n') if len(lcolsplitlines)!=0: lcolsplitlines=lcolsplitlines[len(lcolsplitlines)-1] lcol=len(lcolsplitlines)+1# Ending Column return '{}.{}'.format(srow, scol),'{}.{}'.format(lrow, lcol)#, (lrow, lcol)
def coordinate(pattern, string,txt): line=string.splitlines() start=string.find(pattern) # Here Pattern Word Start end=start+len(pattern) # Here Pattern word End srow=string[:start].count('\n')+1 # starting row scolsplitlines=string[:start].split('\n') if len(scolsplitlines)!=0: scolsplitlines=scolsplitlines[len(scolsplitlines)-1] scol=len(scolsplitlines)# Ending Column lrow=string[:end+1].count('\n')+1 lcolsplitlines=string[:end].split('\n') if len(lcolsplitlines)!=0: lcolsplitlines=lcolsplitlines[len(lcolsplitlines)-1] lcol=len(lcolsplitlines)# Ending Column return '{}.{}'.format(srow, scol),'{}.{}'.format(lrow, lcol)#, (lrow, lcol)
def check(k={}): if k['COMMENT']!=None: return 'comment','red' elif k['BUILTIN']!=None: return 'builtin','VioletRed' elif k['STRING']!=None: return 'string','green' elif k['KEYWORD']!=None: return 'keyword','orange' else: return 'ss','NILL'
txtfilter=re.compile(ty(),re.S)
class ColorLight: def __init__(self, txtbox=None): self.txt=txtbox self.txt.bind("<Any-KeyPress>", self.trigger)
def binding_functions_configuration(self): self.txt.storeobj['ColorLight']=self.trigger return
def trigger(self, event=None): val=self.txt.get('1.0','end') if len(val)==1: return for i in ['comment','builtin','string','keyword']: self.txt.tag_remove(i,'1.0','end') for i in txtfilter.finditer(val): start=i.start() end=i.end()-1 #print start,end tagtype,color=check(k=i.groupdict()) if color!='NILL': ind1,ind2=_coordinate(start,end,val) #print ind1, ind2 self.txt.tag_add(tagtype,ind1, ind2) self.txt.tag_config(tagtype,foreground=color) #Tkinter.Text.tag_configure # for i in idprog.finditer(val): # start=i.start() # end=i.end()-1 # ind1,ind2=_coordinate(start,end,val) # self.txt.tag_add('BLUE',ind1, ind2) # self.txt.tag_config("BLUE",foreground='grey') # #Tkinter.Text.tag_configure
if __name__ == '__main__': root=Tkinter.Tk() txt=Tkinter.Text(root) txt.pack(expand='yes') txt.storeobj={} store=ColorLight(txtbox=txt) Tkinter.Button(root, text='Click me', command=lambda:store.trigger()).pack() root.mainloop()
|