#!/usr/bin/env python # -*- coding: iso-8859-15 -*- # KMB 2008-12-24 # module for GUI text search applications from Tkinter import * from ScrolledText import ScrolledText from tkFont import Font from sys import stderr try: import _winreg win=True except: win=False class App(Frame): def __init__(s,master=None,title='',action=lambda x,**y:x,init=None,fontsize=14,checkbutton=None,help='',fontfamily='Arial'): Frame.__init__(s,master) s.grid() s.font=Font(family=fontfamily,size=fontsize) s.n=0 s.master.title(title) #s.master.winfo_toplevel().resizable(width=True,height=True) #print s.master.winfo_toplevel().resizable() s.action=action s.cb=IntVar() s.createWidgets(checkbutton,help) if init: msg=init() if msg: print>>stderr,msg exit(1) s.special_chars=('æ','Æ','ð','Ð','þ','Þ') def createWidgets(s,checkbutton,help): # search box... s.frame=Frame(s) s.entry=Entry(s.frame,bg='white',font=s.font) s.entry.bind('',s.get_entry) s.entry.grid(row=0,column=0) s.searchButton=Button(s.frame,text='search',command=s.get_entry,font=s.font) s.searchButton.grid(row=0,column=1) ncols=1 if checkbutton: # checkbutton... ncols+=1 s.checkbutton=Checkbutton(s,text=checkbutton,variable=s.cb,command=s.gcb) s.checkbutton.grid(row=1,column=ncols) if help: # help button... ncols+=1 s.helptext=help s.help=Button(s,text='help',command=s.help) s.help.grid(row=1,column=ncols) # quit button... ncols+=1 s.quitButton=Button(s,text='quit',command=s.quit) s.quitButton.grid(row=1,column=ncols) # text... ncols+=1 s.frame.grid(row=1,column=0) s.text=ScrolledText(s,bg='white',fg='blue',font=s.font) s.text.grid(row=0,column=0,columnspan=ncols) def help(s): s.text.insert(INSERT,'%s\n'%(s.helptext,)) s.text.yview(MOVETO,1) def gcb(s): pass def get_entry(s,event=None): word=s.entry.get() result,redden=s.action(word,cb=s.cb.get()) if result: s.text.mark_set('sentinel',INSERT) s.text.mark_gravity('sentinel',LEFT) s.text.insert(INSERT,'%s\n'%(result,)) s.text.yview(MOVETO,1) if redden: s.redden(redden) s.text.mark_unset('sentinel') s.n+=1 def redden(s,red=''): if not red: return start='sentinel' n=0 count=IntVar() while s.text.compare(start,'<',END): ms=s.text.search(red,start,nocase=1,stopindex=END,regexp=1,count=count) if not ms: break end=str(ms)+'+ %d chars'%count.get() s.text.tag_add('red',ms,end) start=end+'+ 1 chars' s.text.tag_config('red',foreground='red') def action(x,cb=None): return x,'abc' if __name__=='__main__': app=App(title='search_gui demo by Keith Briggs',action=action,checkbutton='case sensitive',help='help text') app.mainloop()