[Tkinter-discuss] Text editing and tags...
Vasilis Vlachoudis
Vasilis.Vlachoudis at cern.ch
Wed Dec 12 05:32:24 EST 2018
Thanks Michael,
I am doing something similar, by inserting the tag_names of the
character before like
txt.insert(tk.INSERT, event.char, txt.tag_names(tk.INSERT + "-1c"))
but I don't find it elegant.
I saw that for marks there is a gravity field but it doesn't exist for tags
What it is the advantage of using the
txt.tk.call('tk::TextInsert', txt._w, event.char)# borrowed from text.tcl
instead of txt.insert()?
Thanks
Vasilis
________________________________________
From: Tkinter-discuss [tkinter-discuss-bounces+vasilis.vlachoudis=cern.ch at python.org] on behalf of Michael Lange [klappnase at web.de]
Sent: Tuesday, December 11, 2018 20:54
To: tkinter-discuss at python.org
Subject: Re: [Tkinter-discuss] Text editing and tags...
Hi Vasilis,
On Tue, 11 Dec 2018 13:45:10 +0000
Vasilis Vlachoudis <Vasilis.Vlachoudis at cern.ch> wrote:
> Hi all,
>
> Considering the following code, it creates a Text() and adds the word
> "Red" with tag "red" and "Normal" without afterwords without any tag.
> If you click the cursor in between the two letters d|N from (Red"d" and
> "N"ormal) and you start typing, the newly inserted text will be black
> without out any tag associated. Is there a way to change this behavior
> like the word processing editor where the end of a tag is inclusive
> that if you start typing at the end of the tag it will assume that is
> inside the tag-range and not outside?
so far as I can see there is no built-in way to achieve this. Maybe you
can try to override the default Tk event handlers of the Text widget.
For starters I set up a very basic example of a modified KeyPress event
handler:
##################################
import tkinter as tk
root=tk.Tk()
txt = tk.Text(root)
txt.pack(fill='both', expand=1)
txt.tag_configure("red", foreground="Red")
txt.insert('end',"Red", 'red')
txt.insert('end',"Normal")
def on_key_press(event):
ins = txt.index('insert')
if event.char and not ('\n' in event.char or '\r' in event.char or '\b' in event.char):
txt.tk.call('tk::TextInsert', txt._w, event.char)# borrowed from text.tcl
l, i = str(ins).split('.')
if int(i) > 0 and 'red' in txt.tag_names('%s.%d' %(l, int(i)-1)):
txt.tag_add('red', ins)
return('break')
txt.bind('<KeyPress>', on_key_press)
root.mainloop()
##################################
I guess this is not perfect though, and you will probably also want
handlers for copy-and-paste'ing text.
Best regards
Michael
.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.
It is a human characteristic to love little animals, especially if
they're attractive in some way.
-- McCoy, "The Trouble with Tribbles", stardate 4525.6
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss
More information about the Tkinter-discuss
mailing list