pythontkinterrichtextitalics

Tkinter: italicizing already bold text (overlapping tags)


I have a Python Tkinter program where I can select text, press a shortcut and it will make the selected text bold. You can use different shortcuts for other styles such as italics.

However, I'm having a problem while as a user in the running program where if I try to make already bold text italicized via the shortcut, then the text only appears to be italicized and not both bold and italics. I know this makes sense, because the tags are assigned to do one or the other, and doing both isn't going to merge the effect of the tags. However, I don't know of a way to determine what happens when you have multiple tags on your text.

Can you have a tag that somehow represents the overlap of two other specific tags?

The only way I see to handle this (from what I see in the documentation) is to use Text.tag_bind to bind a function to each of my style tags that does some interesting stuff to make the proper text, and only the proper text, both bold and italic. I suppose that's doable, but if it's not the right way to do it, I'd like to know.

I don't have trouble making a tag that does both bold and italics at once. What I need is to be able to handle overlapped tags.

Here's the pertinent code of what I'm doing already:

def set_tag_styles(self):
    self.myTextWidget.tag_config("bold", font=[self.d["font"][0], self.d["font"][1], "bold"])
    self.myTextWidget.tag_config("italic", font=[self.d["font"][0], self.d["font"][1], "italic"])
    self.myTextWidget.tag_config("underline", font=[self.d["font"][0], self.d["font"][1], "underline"])
    self.myTextWidget.tag_config("overstrike", font=[self.d["font"][0], self.d["font"][1], "overstrike"])
def invert_tag(self, start, end=None, tag=SEL, w=None):
    #This just makes text without the tag have the tag and text with the tag not have the tag anymore.
    if w==None:
        w=self.myTextWidget
    i=0
    if end==None:
        if tag in w.tag_names(start):
            w.tag_remove(tag, start)
        else:
            w.tag_add(tag, start)
    else:
        while w.compare(start+"+"+str(i)+"c", "<", end):
            if tag in w.tag_names(start+"+"+str(i)+"c"):
                w.tag_remove(tag, start+"+"+str(i)+"c")
            else:
                w.tag_add(tag, start+"+"+str(i)+"c")
            i+=1
    self.set_tag_styles()
def bold_text(self, event=None):
    try:
        self.invert_tag("sel.first", "sel.last", "bold")
    except:
        if self.myTextWidget.get("insert wordstart") in {" ", "\n", "\t", "\r", " "}:
            pass
        else:
            self.invert_tag("insert wordstart", "insert wordend", "bold")
    return "break"
def italic_text(self, event=None):
    try:
        self.invert_tag("sel.first", "sel.last", "italic")
    except:
        if self.myTextWidget.get("insert wordstart") in {" ", "\n", "\t", "\r", " "}:
            pass
        else:
            self.invert_tag("insert wordstart", "insert wordend", "italic")
    return "break"

EDIT: For those interested (and not wanting to code everything from scratch), here's the code of what I did to get it to work (using Bryan Oakley's answer as guidance):

self.style_tags={"bold", "italic", "underline", "overstrike", "bold italic", "bold italic underline", "bold italic underline overstrike", "italic underline", "italic overstrike", "italic underline overstrike", "underline overstrike", "bold underline", "bold underline overstrike", "bold overstrike", "bold italic overstrike"};
    …
    def clear_multiple_styles(self, pos, w=None):
        #This gets rid of all multi-style tags (like "bold italic underline").
        if w==None:
            w=self.myTextWidget;
        for x in self.style_tags:
            s=Switch(); #This is my version of a switch statement (so I don't have to type my compare variable every time), with added flexibility.
            s.switch(x);
            if s.neq("bold", "italic", "underline", "overstrike"): #This means, if x isn't equal to any of them
                if x in w.tag_names(pos):
                    w.tag_remove(x, pos);
    def update_style(self, pos, w=None):
        #This updates the styles of an index to take care of overlapping style tags.
        if w==None:
            w=self.myTextWidget;
        self.clear_multiple_styles(pos, w);
        s=Switch();
        s.switch(w.tag_names(pos));
        if s.ins("bold", "italic", "underline", "overstrike"): #i.e. If these args are all in w.tag_names(pos)
            w.tag_add("bold italic underline overstrike", pos);
        elif s.ins("bold", "italic", "underline"):
            w.tag_add("bold italic underline", pos);
        elif s.ins("bold", "italic", "overstrike"):
            w.tag_add("bold italic overstrike", pos);
        elif s.ins("bold", "italic"):
            w.tag_add("bold italic", pos);
        elif s.ins("bold", "underline", "overstrike"):
            w.tag_add("bold underline overstrike", pos);
        elif s.ins("bold", "underline"):
            w.tag_add("bold underline", pos);
        elif s.ins("bold", "overstrike"):
            w.tag_add("bold overstrike", pos);
        elif s.ins("italic", "underline", "overstrike"):
            w.tag_add("italic underline overstrike", pos);
        elif s.ins("italic", "underline"):
            w.tag_add("italic underline", pos);
        elif s.ins("italic", "overstrike"):
            w.tag_add("italic overstrike", pos);
        elif s.ins("underline", "overstrike"):
            w.tag_add("underline overstrike", pos);
    def invert_style_tag(self, start, end=None, tag="bold", w=None):
        if w==None:
            w=self.myTextWidget;
        i=0;
        if end==None:
            if tag in w.tag_names(start):
                w.tag_remove(tag, start);
            else:
                w.tag_add(tag, start);
            self.update_style(start);
        else:
            while w.compare(start+"+"+str(i)+"c", "<", end):
                if tag in w.tag_names(start+"+"+str(i)+"c"):
                    w.tag_remove(tag, start+"+"+str(i)+"c");
                else:
                    w.tag_add(tag, start+"+"+str(i)+"c");
                self.update_style(start+"+"+str(i)+"c");
                i+=1;
        self.set_tag_styles();
    def set_tag_styles(self):
        single_styles={"bold", "italic", "underline", "overstrike"};
        for x in self.style_tags:
            x_list=x.split();
            self.myTextWidget.tag_config(x, font=[self.d["font"][0], self.d["font"][1]]+x_list); #You can add lists together to get the extra arguments in.
            for y in single_styles:
                if x not in single_styles:
                    self.myTextWidget.tag_raise(x); #Gives the multi-style tag higher priority than existing single-style tags
    def style_text(self, style):
        try:
            self.invert_style_tag("sel.first", "sel.last", style);
        except:
            if self.myTextWidget.get("insert wordstart") in {" ", "\n", "\t", "\r", " "}:
                pass;
            else:
                self.invert_style_tag("insert wordstart", "insert wordend", style);
    def bold_text(self, event=None):
        self.style_text("bold");
        return "break";
    def italic_text(self, event=None):
        self.style_text("italic");
        return "break";
    def underline_text(self, event=None):
        self.style_text("underline");
        return "break";
    def overstrike_text(self, event=None):
        self.style_text("overstrike");
        return "break";

For those who want my Switch class code (instead of translating it to standard expressions), here it is (sorry in advance if this is too much code for you):

class Switch:
    def switch(self, item):
        self.item=item;
    def case(self, values, operator="=="):
        #values must be a list, set, tuple or other sequence. This is to allow one not to have to define operator. If you don't like this, use the other methods.
        if operator in "==":
            return self.eq(*values);
        elif operator=="!" or operator=="!=":
            return self.neq(*values);
        elif operator==">":
            return self.gr(*values);
        elif operator=="<":
            return self.ls(*values);
        elif operator==">=":
            return self.gre(*values);
        elif operator=="<=":
            return self.lse(*values);
        elif operator in "range" and operator[0]=="r":
            if len(values)!=2:
                raise ValueError("There must be two and only two values in a range.");
            return self.range(values[0], values[1]);
        elif operator in "nrange" and operator[0]=="n":
            if len(values)!=2:
                raise ValueError("There must be two and only two values in an nrange.");
            return self.nrange(values[0], values[1]);
        else:
            raise ValueError("The operator "+operator+" is not currently defined.");
    def ins(self, *values):
        #If all the values are part of the string or sequence, self.item, return True. Else return False.
        #Note: It doesn't take into account that "" is in every string and some tuples.
        for x in values:
            if x not in self.item:
                return False;
        return True;
    def eq(self, *values):
        #Equal to
        return self.item in values;
    def gr(self, *values):
        #Greater than
        for x in values:
            if self.item<=x:
                return False;
        return True;
    def gre(self, *values):
        #Greater than or equal to
        for x in values:
            if self.item<x:
                return False;
        return True;
    def ls(self, *values):
        #Less than
        for x in values:
            if self.item>=x:
                return False;
        return True;
    def lse(self, *values):
        #Less than or equal to
        for x in values:
            if self.item>x:
                return False;
        return True;
    def neq(self, *values):
        return self.item not in values;
    def range(self, min, max):
        return self.item in range(min, max) or max==self.item or min==self.item;
    def nrange(self, min, max):
        return self.item not in range(min, max) and max!=self.item and min!=self.item;

Solution

  • The problem isn't overlapping tags per se, it's that you're trying to use overlapping fonts. If two tags define a font property, only the font for the tag with the higher priority will be used.

    The only option is to create a third tag for "bold-italic" that has an appropriate font defined. Then, when you want to bold or italicize something, you you'll need to have a special case to handle use the third tag appropriately (ie: if a range has no tags, add italic, if it has bold, change it to bold-italic, etc).