当我将某些python脚本的内容插入到文本小部件中时,内容可以快速突出显示。此外,插入内容后,也可以突出显示手动键入的新插入。但是,在将某些python脚本的内容插入到文本小部件之后,新的插入将被快速插入,但会以延迟的突出显示。
我该怎么做才能解决这个问题?
提前谢谢。
码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
if sys.version_info.major == 2:
exit()
elif sys.version_info.major == 3:
import io
import keyword
import builtins
import tokenize
import threading
import tkinter as tk
root = tk.Tk()
text = tk.Text(master=root, fg="white", bg="black", font="TkDefaultFont 10")
text.pack(fill="both", expand=True)
count = 0
def colorize(*args):
global count
row1, col1 = args[0].start
start = str(row1) + "." + str(col1)
row2, col2 = args[0].end
end = str(row2) + "." + str(col2)
text.tag_add(str(count), start, end)
try:
text.tag_config(str(count), foreground=args[1], font=args[2])
except IndexError:
text.tag_config(str(count), foreground=args[1])
count += 1
def search():
while True:
try:
for i in tokenize.tokenize(io.BytesIO(text.get("1.0", "end").encode("utf-8")).readline):
if i.type == 1:
if i.string in keyword.kwlist:
colorize(i, "orange")
elif i.string in dir(builtins):
colorize(i, "blue")
else:
colorize(i, "white")
elif i.type == 2:
colorize(i, "cyan")
elif i.type == 3:
colorize(i, "purple")
elif i.type == 53:
if i.string == "," or i.string == "." or i.string == ":":
colorize(i, "orange")
elif i.string == "(" or i.string == ")" or i.string == "[" \
or i.string == "]" or i.string == "{" or i.string == "}":
colorize(i, "darkred")
else:
colorize(i, "green")
elif i.type == 57:
colorize(i, "grey", "TkDefaultFont 10 italic")
except tokenize.TokenError:
pass
thread = threading.Thread(target=search)
thread.daemon = True
thread.start()
thread.join(1)
root.mainloop()发布于 2018-06-15 15:18:34
问题解决了。
从代码中删除了线程模块。现在,解析操作是用键绑定("<KeyRelease>")完成的。因此,search()函数中的while循环也被删除。
colorize(*args)函数被更改为:
def colorize(*args):
global count
row = text.index("insert").split(".")[0]
col1 = args[0].start[-1]
start = row + "." + str(col1)
col2 = args[0].end[-1]
end = row + "." + str(col2)
text.tag_add(str(count), start, end)
try:
text.tag_config(str(count), foreground=args[1], font=args[2])
except IndexError:
text.tag_config(str(count), foreground=args[1])
count += 1根据此函数,行变量设置为insert位置,以停止对整个内容重复着色。因此,只需要扫描最后一行并对其着色..
针对不同的情况定义了两种解析方法。
当插入位置位于最后一行时,第一个解析器将被激活。第一个解析器在我上一篇文章中的search()方法的核心部分没有更改。第二个解析器在要将复制的代码粘贴到小部件中时被激活。
下面是第二个解析器方法:
keysym = set()
def parser_2(*args):
global keysym
keysym = set()
for i in range(int(args[0])):
text.mark_set("insert", "{}.{}".format(i + 1, args[1]))
parser_1(text.get("{}.0".format(i + 1), "{}.0".format(i + 2)))
text.mark_set("insert", "{}.{}".format(args[0], args[1]))将复制的代码插入小部件后,插入位置将如预期的那样发生变化。因此,如果我们直接或反向访问for循环中的所有行(或反向),并设置所有这些已访问行的标记,并使用参数调用parser_1(*args)函数(参数被访问行和插入位置的列),然后再次将标记设置为正常插入位置,则复制的内容可以着色一次。当用户键入ctrl+v或用户希望使用右键菜单将代码粘贴到小部件时,这是一个快捷函数。
最后一个函数(select_parser(event))是根据不同的情况选择解析器。
def select_parser(event):
row, col = text.index("insert").split(".")
if event.keysym == "Control_L":
keysym.add(event.keysym)
elif event.keysym == "v" or event.keysym == "V":
keysym.add(event.keysym)
if "Control_L" in keysym:
parser_2(row, col)
elif event.keysym == "Control_R":
keysym.add(event.keysym)
if "v" in keysym or "V" in keysym:
parser_2(row, col)
else:
parser_1(text.get("{}.0".format(row), "{}.0".format(int(row) + 1)))一种情况是当用户键入"Control_L".时如果用户键入它,则将其添加到在全局中定义的keysym集中。
另一种情况是当用户键入"v"或"V"时。如果用户键入其中之一,event.keysym也会添加到keysym集中。在本例中定义了一个不同的条件,用于检查"Control_L"是否在keysym集合中。如果是in,则调用第二个解析方法。并在第二个解析器方法中重新定义keysym。
另一种情况是当用户键入"Control_R".时如果用户键入它,它也会添加到keysym集中。其中定义的另一个条件是检查"v"还是"V"是否在keysym集合中。(当我们键入"Control_R + v"时,第一个"v"被添加到keysym集合中,但是当我们键入"Control_L + v"时,第一个“Control_L”E 251被添加到E 152keysymE 253集合中。)如果"v"或"V"位于keysym集合中,则调用第二个解析方法。并在第二个解析器方法中重新定义keysym。
最后一种情况是当用户键入与上面的键不同的时候。在这种情况下,调用第一个解析器方法。
码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
if sys.version_info.major == 2:
exit()
elif sys.version_info.major == 3:
import io
import keyword
import builtins
import tokenize
import tkinter as tk
root = tk.Tk()
text = tk.Text(master=root, fg="white", bg="black", font="TkDefaultFont 10")
text.pack(fill="both", expand=True)
count = 0
def colorize(*args):
global count
row = text.index("insert").split(".")[0]
col1 = args[0].start[-1]
start = row + "." + str(col1)
col2 = args[0].end[-1]
end = row + "." + str(col2)
text.tag_add(str(count), start, end)
try:
text.tag_config(str(count), foreground=args[1], font=args[2])
except IndexError:
text.tag_config(str(count), foreground=args[1])
count += 1
def parser_1(*args):
try:
for i in tokenize.tokenize(io.BytesIO(
args[0].encode("utf-8")).readline):
if i.type == 1:
if i.string in keyword.kwlist:
colorize(i, "orange")
elif i.string in dir(builtins):
colorize(i, "blue")
else:
colorize(i, "white")
elif i.type == 2:
colorize(i, "cyan")
elif i.type == 3:
colorize(i, "purple")
elif i.type == 53:
if i.string == "," or i.string == "." or i.string == ":":
colorize(i, "orange")
elif i.string == "(" or i.string == ")" or i.string == "[" \
or i.string == "]" or i.string == "{" or i.string == "}":
colorize(i, "darkred")
else:
colorize(i, "green")
elif i.type == 57:
colorize(i, "grey", "TkDefaultFont 10 italic")
except tokenize.TokenError:
pass
keysym = set()
def parser_2(*args):
global keysym
keysym = set()
for i in range(int(args[0])):
text.mark_set("insert", "{}.{}".format(i + 1, args[1]))
parser_1(text.get("{}.0".format(i + 1), "{}.0".format(i + 2)))
text.mark_set("insert", "{}.{}".format(args[0], args[1]))
def select_parser(event):
row, col = text.index("insert").split(".")
if event.keysym == "Control_L":
keysym.add(event.keysym)
elif event.keysym == "v" or event.keysym == "V":
keysym.add(event.keysym)
if "Control_L" in keysym:
parser_2(row, col)
elif event.keysym == "Control_R":
keysym.add(event.keysym)
if "v" in keysym or "V" in keysym:
parser_2(row, col)
else:
parser_1(text.get("{}.0".format(row), "{}.0".format(int(row) + 1)))
text.bind("<KeyRelease>", select_parser)
root.mainloop()https://stackoverflow.com/questions/50837855
复制相似问题