faqts : Computers : Programming : Languages : Python : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 23 people (74%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Is there a way to make a text box uneditable by the user, but insertable ( insert(END, 'schweet') ) by the application?

Sep 27th, 2002 11:31
Greg Goodman, unknown unknown, richard_chamberlain


There may be a better way of doing it...
from Tkinter import *
root=Tk()
text=Text(root,width=50,height=50)
text.pack()
def intercept(event):
    return 'break'
text.bind('<Key>',intercept)
text.insert(END,'Sweet ;-)')
root.mainloop()
Here I bind a <Key> event to the intercept function - where I return 
'break' to stop the event propagating, so basically it's disabled. To 
undisable you'd just need to unbind that event.
---
You could create a disabled Text widget and bracket the programmatic
text insertion with calls to enable/disable the widget:
from Tkinter import *
root = Tk()
w = Text(root, width=50, height=5, state=DISABLED)
w.pack()
def do_insert():
	w.configure(state=NORMAL)
	w.insert(END, 'more text...')
	w.configure(state=DISABLED)
Button(root, text='insert', command=do_insert).pack()
root.mainloop()