i got a working highlight code example that works in python 2.7.2
import Tkinter
"""
Edit a file and save the text.
"""
textFont1 = ("Courier New", 16, "normal")
class ScrollbarX(Tkinter.Scrollbar):
def set(self, low, high):
if float(low) <= 0.0 and float(high) >= 1.0:
self.grid_remove()
else:
self.grid()
Tkinter.Scrollbar.set(self, low, high)
class App(Tkinter.Tk):
def __init__(self, fn, fnout):
Tkinter.Tk.__init__(self)
self.title("Text Widget")
self.fin = open(fn, 'r')
self.fnout = fnout
self.mainFrame = Tkinter.Frame(self)
top=self.winfo_toplevel()
top.columnconfigure(0, weight=1)
top.rowconfigure(0, weight=1)
self.mainFrame.grid(row=0, column=0, sticky="nsew")
self.exit = Tkinter.Button(self.mainFrame,
text="Save and Exit",
command=self.finish)
self.exit.grid(row=0, column=0, sticky="ns")
self.mainFrame.columnconfigure(0, weight=1)
self.mainFrame.rowconfigure(1, weight=1)
vscrollbar = ScrollbarX(self.mainFrame)
vscrollbar.grid(row=1, column=1, sticky="ns")
hscrollbar = ScrollbarX(self.mainFrame, orient=Tkinter.HORIZONTAL)
hscrollbar.grid(row=2, column=0, sticky="ew")
self.textWidget = Tkinter.Text(self.mainFrame,
yscrollcommand=vscrollbar.set,
xscrollcommand=hscrollbar.set,
wrap=Tkinter.NONE,
height=24,
width=60,
font=textFont1)
self.textWidget.insert("1.0", self.fin.read())
self.textWidget.grid(row=1, column=0, sticky="nsew")
hscrollbar["command"] = self.textWidget.xview
vscrollbar["command"] = self.textWidget.yview
self.Btn1 = Tkinter.Button(self.mainFrame,
text="Highlight 'bolt'",
command=lambda: self.highlight("bolt"))
self.Btn1.grid(row=3, column=0, sticky="ns")
def finish(self):
fout = open(self.fnout, 'w')
fout.write(self.textWidget.get("1.0", "end"))
fout.close()
self.fin.close()
app.destroy()
def highlight(self, seq):
if "highlight" in self.textWidget.tag_names():
self.textWidget.tag_delete("highlight")
i = len(seq)
idx = "1.0"
while True:
idx = self.textWidget.search(seq, idx, nocase=1, stopindex='end')
if idx:
idx2 = self.textWidget.index("%s+%dc" % (idx, i))
self.textWidget.tag_add("highlight", idx, idx2)
self.textWidget.tag_config("highlight", background="yellow")
idx = idx2
else: return
if __name__ == "__main__":
fn = "text1.txt"
fnout = "text2.txt"
app = App(fn, fnout)
app.mainloop()
now i get the highlight function and the highlight button from teh above code and add it on to my program in python 3 but it dont work ?
#!/usr/bin/env python
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import os
import urllib.request
from tkinter.filedialog import asksaveasfilename
def highlight(seq):
#remove highlights
if "highlight" in tkinter.Text.tag_names():
Text.tag_delete("highlight")
i = len(seq)
index = "1.0"
while True:
index = tkinter.Text.search(seq, index, nocase=1, stopindex='end')
if index:
index2 = tkinter.Text.index("%s+%dc" % (index, i))
tkinter.Text.tag_add("highlight", index, index2)
tkinter.Text.tag_config("highlight", background="yellow")
index = index2
else: return
app = Tk()
app.title(" text editor")
content = ttk.Frame(app, padding=(3,3,12,12))
content.grid(column=0, row=0,sticky=(N, S, E, W))
button1 = ttk.Button(content,text="highlight", command=lambda: highlight("s"))
button1.grid(column=3,row=0, columnspan=1, rowspan=2, sticky=(N,W))
Text = tkinter.Text(content,wrap=CHAR, width=50, height=20)
scroll = tkinter.Scrollbar(content,borderwidth=2)
scrollh = tkinter.Scrollbar(content,borderwidth=2, orient=HORIZONTAL)
scrollh.config(command=Text.xview)
Text.config(xscrollcommand=scrollh.set)
scroll.config(command=Text.yview)
Text.config(yscrollcommand=scroll.set, wrap=tkinter.NONE,)
Text.grid(row=2, column=1,columnspan=1, rowspan=3, sticky=(N))
scroll.grid(row=2,column=2, sticky='ns', rowspan=3)
scrollh.grid(row=6, rowspan=1, column=1, sticky='ew')
app.mainloop()