1

In Text Widget when I delete all text with Ctrl + A + DEL, the tag is deleted.

How to fix it?

This is tag code:

def _ingrandisci(self,event=None):
    BloccoNote._c+=1
    self._testo.tag_config("i", font=("Consolas", BloccoNote._c))
    self._testo.tag_add("i", "1.0", "end")
    self._testo.tag_raise("i")
stovfl
  • 14,998
  • 7
  • 24
  • 51
antonio
  • 59
  • 6

2 Answers2

3

Question: Text Widget Ctrl+A + DEL - Reset Formating

  1. Initalize your Text object.
    Bind the <Delete> key to a function. I assume you have this allready done.

    class Text(tk.Text):
        def __init__(self, parent):
            super().__init__(parent)
    
            # Binding Shortcuts
            self.master.bind("<Delete>", self.Delete_func)
    
  2. At deleting all, bind any keyboard input to the set_default_tag function.
    This function are unbound at the first event.char.

        def Delete_func(self, event):
            def set_default_tag(event):
                if event.char:
                    self.master.unbind('<Key>', self.Key_funcid)
    
                self.text.tag_add("i", "1.0", "end")
    
            self.text.delete('1.0', 'end')
            self.Key_funcid = self.master.bind('<Key>', set_default_tag)
    

Tested with Python: 3.5

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • "if not event is None and event.char:" is equal to "if event is None or event.char:" right? – antonio May 04 '19 at 17:37
  • @antonio *"is equal to "if event is None or event.char"*: **No**, you can shorten to `if event.char:`. It's left from testing. The purpose is: **only** stop `''` if got the **first** char from keyboard. – stovfl May 04 '19 at 18:17
  • sorry for disturbing, is there a way to remove only selected text? self.text.delete('1.0', 'end') – antonio May 07 '19 at 20:44
  • @antonio I think this will do: [Tkinter.Text.tag_delete-method](http://effbot.org/tkinterbook/text.htm#Tkinter.Text.tag_delete-method) – stovfl May 07 '19 at 20:50
1

Your observation is incorrect. If you configure a tag and then delete all text, the tag still exists. You are able to use the tag on additional text without having to recreate the tag.

If you manually insert text it won't automatically get the tag since tkinter has no way of knowing what tags to use. Tkinter will only add the tags that are on the character before and after the insertion point. Since there are no characters before or after the insertion point, the new text won't get any tags.

When you are manually editing the text widget, all text goes through the underlying insert method. The documentation for the insert message includes this:

If there is a single chars argument and no tagList, then the new text will receive any tags that are present on both the character before and the character after the insertion point; if a tag is present on only one of these characters then it will not be applied to the new text.

Note: when you press a key in the text widget, it calls the insertmethod with no tagList. For example, pressing the "a" key on the keyboard results in insert("insert", "a") (ie: there is no tagList argument)

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • @antonio: I'm not sure what you're asking. It's possible to have the text widget use any tags you want on insert. It requires work, but it's possible. That's not what you asked, however. You asked how to fix the tag being deleted. Since that tag isn't being deleted, there's no fix for that. – Bryan Oakley May 01 '19 at 19:44