我正在编写过去20年中的第一个python应用程序。我对Tkinter Text小部件的tag_add()函数有问题。添加标签仅在第一次有效,但第二次无效。我用tag_names()检查了我的标签是否在取消选中“突出显示错误”复选框时被删除了。并被删除。再次单击该检查按钮,甚至可以重新添加该文本,但是在第二次尝试中文本未着色。
有人知道吗? 作为多年来的第一个python代码,您是否对我的实现和结构化方式有任何反馈? (抱歉无法摆脱CamelCase)
预先感谢 SLi
from Tkinter import Tk, BOTH, END, N, W, S, TOP, BOTTOM, INSERT, LEFT, RIGHT, SUNKEN, RAISED, X, Y, PanedWindow, Frame, LabelFrame, Scrollbar, Checkbutton, Entry, Button, Label, Text, Menu, IntVar
from ScrolledText import ScrolledText
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initializeUiVariables()
self.doWindowSetup()
self.createWidgets()
self.doColorSetup()
def doWindowSetup(self):
self.parent.title('PACE Client Log Viewer')
self.screenWidth = self.parent.winfo_screenwidth()
self.screenHeight = self.parent.winfo_screenheight()
desiredWindowWidth = (self.screenWidth * 1.0)
desiredWindowHeight = (self.screenHeight * 1.0)
x = (self.screenWidth / 2) - (desiredWindowWidth / 2)
y = (self.screenHeight / 2) - (desiredWindowHeight / 2)
self.parent.geometry('%dx%d+%d+%d' % (desiredWindowWidth, desiredWindowHeight, x, y))
def initializeUiVariables(self):
self.fontSize = 12
self.highlightErrors = IntVar()
def createWidgets(self):
panedWindow = PanedWindow(sashrelief=RAISED)
panedWindow.pack(fill=BOTH, expand=1)
self.wText = ScrolledText(panedWindow)
self.wText.config(font=("consolas", self.fontSize))
self.wText.pack(side=LEFT, fill=BOTH, expand=True)
panedWindow.add(self.wText, minsize=(self.screenWidth * 0.75))
self.wText.insert(END, "2018-09-28 11:15:03 GMT - my.app.id (ERROR): Class:CertChecker:error: No certificate loaded. Load certificate before continuing.\n2018-09-28 11:15:07 GMT - my.app.id (INFO): Class:PerformInitialization: begin - version 0.3.10")
frameToolbar = Frame(panedWindow, padx=10)
frameToolbar.pack(side=LEFT, fill=BOTH, expand=True)
panedWindow.add(frameToolbar)
# Highlight Options
frameHighlightOptions = LabelFrame(frameToolbar, text="Highlight", padx=5, pady=5)
frameHighlightOptions.pack(side=TOP, fill=BOTH)
cbErrors = Checkbutton(frameHighlightOptions, text="Errors", anchor=W, padx=5, justify=LEFT, variable=self.highlightErrors, command=self.onHighlightErrors)
cbErrors.pack(side=TOP, fill=X)
def doColorSetup(self):
self.wText.tag_config("highlightError", background="#EE2C2C", foreground="#FFFFFF") # red
def onHighlightErrors(self):
if self.highlightErrors.get() == 0:
self.wText.tag_delete("highlightError")
else:
self.highlightRow("error", "highlightError")
def highlightRow(self, pattern, tag):
self.highlightPattern(pattern, tag, True)
def highlightPattern(self, pattern, tag, highlightRow=False):
start = self.wText.index("1.0")
end = self.wText.index(END)
self.wText.mark_set("matchStart", start)
self.wText.mark_set("matchEnd", start)
self.wText.mark_set("searchLimit", end)
count = IntVar()
while True:
index = self.wText.search(pattern, "matchEnd","searchLimit", count=count, regexp=True, nocase=True)
if index == "": break
if count.get() == 0: break # degenerate pattern which matches zero-length strings
if highlightRow:
row, col = index.split('.')
self.wText.mark_set("matchStart", "%s.%s" % (int(row), 0))
else:
self.wText.mark_set("matchStart", index)
if highlightRow:
lineEndIndex = self.wText.search("\n", index, "searchLimit", count=count, regexp=False, nocase=False)
row, col = lineEndIndex.split('.')
self.wText.mark_set("matchEnd", lineEndIndex)
else:
self.wText.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
self.wText.tag_add(tag, "matchStart", "matchEnd")
def main():
root = Tk()
ex = Application(root)
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
删除标签时,将破坏与该标签关联的所有信息。下次添加标签时,它没有与之关联的颜色,因此看不到标签。
与其删除标签,不如将其从文本中删除。
替换此:
self.wText.tag_delete("highlightError")
...与此:
self.wText.tag_remove("highlightError", "1.0", "end")