我制作了一个Python GUI,让用户输入记事本中的内容
然后我添加了一个线程,检查是否找到关键字input
并将其标记为蓝色
它标记了import
关键字,但不是完整的字词......最后只有't'
所以我决定制作一个输出,打印所有彩色关键字,并将其开头line.char
(i
导入) - >到最后line.char
(t
进口)
它把所有东西都拿出来了,输出也没问题。但是标记仍然不起作用
有人有理由,为什么会这样?
我的代码用于创建文本小部件并添加标记:
def createWidgets(self):
self.code = tkinter.Text(self)
self.code.tag_configure("BLUE", foreground="blue")
我的线程代码:
def startT(self):
for i in range(5):
t = threading.Thread(target=self.markup)
t.start()
我的标记代码:
def markup(self):
while True:
time.sleep(0.1)
#print("Getting code..")
currentline = 0
for line in self.code.get("1.0","end").splitlines():
currentline += 1
content = line.split(" ")
for word in content:
if word == "import":
try:
self.code.tag_add("BLUE", getBegin(currentline, line, word), getEnd(currentline, line, word))
print("Set \""+word+"\" from "+getBegin(currentline, line, word)+" TO "+getEnd(currentline, line, word)+ " to BLUE!")
except tk.TclError:
pass
else:
try:
self.code.tag_add("BLACK", getBegin(currentline, line, word), getEnd(currentline, line, word))
print("Set \""+word+"\" from "+getBegin(currentline, line, word)+" TO "+getEnd(currentline,line,word)+" to BLACK!")
except tk.TclError:
pass
最后我获得了这个职位的代码:
def getBeginEnd(line, word):
buffer = 0
for inline in line.split(" "):
if inline != word:
buffer += len(inline)+1
else:
return str(buffer)
def getBegin(currentline, line, word):
return str(currentline)+"."+getBeginEnd(line,word)
def getEnd(currentline, line, word):
return str(currentline)+"."+str(int(getBeginEnd(line,word))+len(word))