[Tkinter-discuss] Editing text in Canvas

Michael Lange klappnase at web.de
Sat Aug 27 10:57:15 CEST 2011


Hi,

Thus spoketh Martin B <spooky.ln at tbs-software.com> 
unto us on Fri, 26 Aug 2011 22:26:16 +0200:

> Thanks for info.
> Yes i slowly updating source. using web for my backup sometimes.
> 
> Now when i arrive from my bike trip with clean head i tried use
> 'unbind' in __edit_value method and created new bind in __select value.
> seems to work.
> 
> as for entry. i dont know how i may change background under entry if
> filled rect is under object.

You are right, this is not possible. Maybe a way to go is to temporarily
create an Entry, as in this example:

from Tkinter import *
root = Tk()
c = Canvas(root)
c.pack()
textvar = StringVar()
t = c.create_text(100, 100, text='foo', anchor='nw')

def edit_begin(event):
    x, y = c.coords(t)
    textvar.set(c.itemcget(t, 'text'))
    e = Entry(c, width=5, textvariable=textvar, bd=0,
                highlightthickness=0, bg=c['bg'])
    e.selection_range(0, 'end')
    w = c.create_window(x, y, window=e, tags=('editwindow',), anchor='nw')
    e.focus_set()
    e.bind('<Return>', edit_end)
    e.bind('<Escape>', edit_cancel)

def edit_cancel(event):
    c.delete('editwindow')
    event.widget.destroy()
    c.focus_set()

def edit_end(event):
    c.itemconfigure(t, text=textvar.get())
    edit_cancel(event)

c.bind('<Control-e>', edit_begin)

c.focus_set()
root.mainloop()

Regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Earth -- mother of the most beautiful women in the universe.
		-- Apollo, "Who Mourns for Adonais?" stardate 3468.1


More information about the Tkinter-discuss mailing list