[Tkinter-discuss] tag insertion while writing

Michael Lange klappnase at web.de
Wed Oct 5 16:48:13 CEST 2011


Hi,

Thus spoketh Alessandro Magni <alexxx.magni at gmail.com> 
unto us on Wed, 5 Oct 2011 14:40:30 +0200:

> Hi everybody,
> I'm looking for some directions, while programming a very simple
> editor (supporting basic formatting)
> 
> The window where the user writes is a Text widget (named: text), where
> I put a tag 'b' to set the text where this tag applies to bold.
> 
> This is not a problem when I apply the bold to a selection:
> 
> text.tag_add('b', SEL_FIRST,SEL_LAST)
> 
> I have instead two problems when I just want to switch on/off the bold
> while typing. To switch it on the only way I found is this:
> 
> text.insert(INSERT, '  ', 'b' )
> text.mark_set("insert", INSERT+'-1c')
> 
> notice that I have to insert TWO spaces. If I insert one, the bold
> doesnt apply. If I insert '', my cursor goes back one char!
> 
> My second problem is how to switch it off, when I'm writing within a
> bolded region - and for this I havent the slightest idea...

The following appears to work here:

>>> t = Text(font='courier -12')
>>> t.pack(fill='both', expand=1)
>>> t.tag_configure('b', font='courier -12 bold')
>>> t.tag_add('b', 'insert-1c', 'end')
... type something, it is bold...
>>> t.tag_remove('b', 'insert', 'end')
... now it is normal again...

except that it doesn't seem to be possible to begin editing an empty
text widget with the bold font and that on tag_add() the character
*before* the insertion cursor is bold'ed which looks a little odd to me.
So, you have to type the first character that is supposed to be bold and
then switch the font.
OTOH, if you bind these commands to some key events it might be something
that one can live with.

Best regards

Michael


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

The games have always strengthened us.  Death becomes a familiar
pattern.  We don't fear it as you do.
		-- Proconsul Marcus Claudius, "Bread and Circuses",
		   stardate 4041.2


More information about the Tkinter-discuss mailing list