John Posner wrote:
Dave, you're doing exactly the right thing: gradually expanding your
program, to provide more functionality and to learn more about the
available programming tools. It's also very good that you take care to
close() the file after processing it. Now for the bad news ...
Seems like there is always bad news :)
1. Don't use "file" as a variable name -- it's a built-in object type.
(Some people don't like the fact that Python allows you to redefine
such "reserved words".)
2. It's probably not the best idea to use a single variable (you use
"file") to do double-duty: to hold the name of a file, and to hold the
open-file object returned by the open() function. It's perfectly
legal, but it hides information that might be useful when you're
debugging a program. This is better:
3. It might be better to use read() rather than readlines() to process
the "red.txt" file. It depends on what that file is supposed to
contain. For example, if you expect "red.txt" to contain exactly one
line, which has one or more words, you can process the open-file
object like this:
All noted and fixed.
It's certainly a mistake to use the expression "str(rList).split()".
Using str() to convert the list "rList" into a string creates a mess
that includes square-bracket characters. Did this actually work for you?
It sort of worked. With one color file it seemed fine but after I
posted I added another color file and things fell apart.
Now with the above fixes it works with three colors from three files.
When the list are printed to the shell the list look like this:
redList ['red', 'dog', 'apple', '#']
blueList ['blue', 'ball', 'berry']
greenList ['green', 'grass', 'do']
But another problem is noticed. It does not matter if the list is built
in code or from a file.
If dog is entered, "do" will be green with the "g" being red.
Back to the drawing board.....
Here is the complete code"
######
from Tkinter import *
import re
RFfile = 'red.txt'
inpRF = open(RFfile,"r")
rList = inpRF.read()
inpRF.close()
BFfile = 'blue.txt'
inpBF = open(BFfile,"r")
bList = inpBF.read()
inpBF.close()
GFfile = 'green.txt'
inpGF = open(GFfile,"r")
gList = inpGF.read()
inpGF.close()
def get_complete_text(event):
complete_text = Tbox.get("1.0", END)
redList = str(rList).split()
blueList = str(bList).split()
greenList = str(gList).split()
print "redList",redList
print "blueList",blueList
print "greenList",greenList
Tbox.tag_remove("red", "1.0", END)
Tbox.tag_remove("blue", "1.0", END)
Tbox.tag_remove("green", "1.0", END)
####RED########
redList_regexp = "|".join(redList)
for matchobj in re.finditer(redList_regexp, complete_text):
start,end = matchobj.span()
Tbox.tag_add("red", "1.0 + %d chars" % start,"1.0 + %d chars" % end)
Tbox.tag_config("red", foreground="red")
####BLUE#######
blueList_regexp = "|".join(blueList)
for matchobj in re.finditer(blueList_regexp, complete_text):
start,end = matchobj.span()
Tbox.tag_add("blue", "1.0 + %d chars" % start,"1.0 + %d chars" %
end)
Tbox.tag_config("blue", foreground="blue")
####GREEN#######
greenList_regexp = "|".join(greenList)
for matchobj in re.finditer(greenList_regexp, complete_text):
start,end = matchobj.span()
Tbox.tag_add("green", "1.0 + %d chars" % start,"1.0 + %d chars"
% end)
Tbox.tag_config("green", foreground="green")
root = Tk()
Tbox = Text(root, width=40, height=15, wrap=CHAR,
font="Times 14 bold", bg="#dddddd")
Tbox.pack()
Tbox.bind("<KeyRelease>", get_complete_text)
Tbox.focus()
root.mainloop()
####
Thanks again,
Dave
--
http://mail.python.org/mailman/listinfo/python-list