- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 Python Tkinter 程序,我可以在其中选择文本,按快捷键,它将使所选文本变为粗体。您可以对其他样式(例如斜体)使用不同的快捷方式。
但是,作为正在运行的程序中的用户,我遇到了一个问题,如果我尝试通过快捷方式将已经粗体的文本设置为斜体,则文本仅显示为斜体,而不是粗体和斜体。我知道这是有道理的,因为标签被分配执行其中一项操作,并且同时执行两项操作不会合并标签的效果。但是,我不知道如何确定当文本上有多个标签时会发生什么。
您能否有一个标记以某种方式表示其他两个特定标记的重叠?
我看到处理这个问题的唯一方法(从我在文档中看到的)是使用 Text.tag_bind
将一个函数绑定(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;
最佳答案
问题不在于重叠标签本身,而在于您尝试使用重叠字体。如果两个标签定义了字体属性,则仅使用优先级较高的标签的字体。
唯一的选择是为“bold-italic”创建第三个标记,并定义适当的字体。然后,当您想要将某些内容加粗或斜体时,您需要有一个特殊的情况来处理适当使用第三个标签(即:如果范围没有标签,则添加斜体,如果有粗体,请将其更改为粗体-斜体等)。
关于python - Tkinter:斜体已经粗体的文本(重叠标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24423996/
下面是我用来制作 1px 文本描边轮廓的代码。但是如何使轮廓变粗呢?如果我只是用“5px”替换所有“1px”,结果看起来很疯狂。 HTML Hello! CSS .element { color:
我是一名优秀的程序员,十分优秀!