tag insertion while writing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

tag insertion while writing

Alessandro Magni-2
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...

Thanks for any help!


alessandro
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: tag insertion while writing

Michael Lange
Hi,

Thus spoketh Alessandro Magni <[hidden email]>
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
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: tag insertion while writing

Michael Lange
Hi again,

Thus spoketh Michael Lange <[hidden email]>
unto us on Wed, 5 Oct 2011 16:48:13 +0200:

(...)
> The following appears to work here:
>
(...)

I found a much better implementation here:

http://wiki.tcl.tk/4018

"Translated" into python this might look like:

####################
root = Tk()
t = Text(root, font='courier -14')
t.pack(fill='both', expand=1)
t.tag_configure('b', font='courier -14 bold')

def modTextInsert(event):
    if event.char == '' or event.widget['state'] == 'disabled':
        return
    try:
        if event.widget.compare('sel.first', '<=', 'insert') and \
                        event.widget.compare('sel.last', '>=', 'insert'):
            event.widget.delete('sel.first', 'sel.last')
    except TclError:
        pass
    event.widget.insert('insert', event.char,
                        event.widget.tag_names('insert-1c'))
    event.widget.see('insert')

for seq in root.bind_class('Text'):
    root.bind_class('modText', seq, root.bind_class('Text', seq))
root.bind_class('modText', '<Key>', modTextInsert)
t.bindtags((t, 'modText', t.winfo_toplevel(), 'all'))

def bold(event):
    t.tag_add('b', 'insert-1c')
def normal(event):
    t.tag_remove('b', 'insert-1c')
t.bind('<F1>', bold)
t.bind('<F2>', normal)

root.mainloop()
####################

This seems to work quite well, except that the changed tag still
applies to the character left to the cursor and that still the handling of
<<Paste>> events is missing ;)

Regards

Michael


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

Lots of people drink from the wrong bottle sometimes.
                -- Edith Keeler, "The City on the Edge of Forever",
                   stardate unknown
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss