我有一个 Python Tkinter 程序,我可以在其中选择文本,按下快捷方式,它将使所选文本变为粗体。您可以对其他样式使用不同的快捷方式,例如斜体。
但是,作为正在运行的程序中的用户,我遇到了一个问题,如果我尝试通过快捷方式将已经粗体的文本变为斜体,那么文本只会显示为斜体,而不是粗体和斜体。我知道这是有道理的,因为标签被分配做一个或另一个,并且两者都做不会合并标签的效果。但是,我不知道如何确定当您的文本上有多个标签时会发生什么。
你能有一个标签以某种方式代表两个其他特定标签的重叠吗?
我看到处理这个问题的唯一方法(从我在文档中看到的)是使用Text.tag_bind
将一个函数绑定到我的每个样式标签,这些标签做一些有趣的事情来制作正确的文本,并且只有正确的文本,包括粗体和斜体。我想这是可行的,但如果这不是正确的方法,我想知道。
我可以轻松制作一个同时显示粗体和斜体的标签。我需要的是能够处理重叠的标签。
这是我已经在做的相关代码:
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"
编辑:对于那些感兴趣的人(并且不想从头开始编写所有代码),这是我为使其工作所做的代码(使用 Bryan Oakley 的回答作为指导):
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";
对于那些想要我的 Switch 类代码(而不是将其转换为标准表达式)的人,这里是(如果这对您来说代码太多,请提前抱歉):
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;