I already looked through the solution to a similar problem here
How can you mark a portion of a text widget as readonly?
but i have tried to make it a little dynamic. Scenario is that a line marked 'readonly'
can be changed later in the program based on few condition.
This is my code below and it is calling the READONLY
Class written by Bryan Oakley.
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
global text
text = ReadonlyText(self)
sb = Scrollbar(self, orient="vertical", command=text.yview)
text.configure(state=DISABLED)
text.configure(yscrollcommand=sb.set)
sb.pack(side="left", fill="y")
text.pack(side="right", fill="both", expand=True)
count = 1.0
with open('chem_data2.txt') as f:
for line in f:
if count == 5.0:
text.insert(str(count), line,'readonly')
else:
text.insert(str(count), line, 'readonly')
count = count + 1.0
f.close()
text.bind('<Key>', self.keyrelease)
pos = text.index('end')
text.tag_configure("readonly", foreground="grey")
def keyrelease(self,event):
text.configure(state=NORMAL)
index = text.index(INSERT)
pos = int(float(index))
let = str(float(pos))
word = text.get(str(float(pos)), str(pos) + '.end')
print pos
#pos = text.index('end')
if float(pos) == 5.0:
print 'i got here'
#text.insert(str(5.0), 'line', 'read')
The above code takes a file as an input and marks each line of the file 'readonly'
at the initial stage but if the user cursor is at the 5th line the line should change to edittable.