Python 用Tkinter文本编辑器突出显示错误
所以我用Python 用Tkinter文本编辑器突出显示错误,python,html,user-interface,tkinter,Python,Html,User Interface,Tkinter,所以我用Tkinter编写了一个文本编辑器。我有一个txt文件,其中存储了所有HTML标记。不幸的是,当我试图通过逐行读取文件来为所有标记实现语法高亮显示时,标记之外的所有其他内容都会高亮显示。这很奇怪,因为当我单独突出显示每个标记时,这个问题不会出现 import tkinter as tk class CustomText(tk.Text): def __init__(self, *args, **kwargs): tk.Text.__init__(self, *
Tkinter
编写了一个文本编辑器。我有一个txt
文件,其中存储了所有HTML
标记。不幸的是,当我试图通过逐行读取文件来为所有标记实现语法高亮显示时,标记之外的所有其他内容都会高亮显示。这很奇怪,因为当我单独突出显示每个标记时,这个问题不会出现
import tkinter as tk
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
def highlight_pattern(self, pattern, tag, start="1.0", end="end",
regexp=False):
'''Apply the given tag to all text that matches the given pattern
If 'regexp' is set to True, pattern will be treated as a regular
expression.
'''
start = self.index(start)
end = self.index(end)
self.mark_set("matchStart", start)
self.mark_set("matchEnd", start)
self.mark_set("searchLimit", end)
count = tk.IntVar()
while True:
index = self.search(pattern, "matchEnd","searchLimit",
count=count, regexp=regexp)
if index == "": break
self.mark_set("matchStart", index)
self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
self.tag_add(tag, "matchStart", "matchEnd")
class Arshi(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.createtext()
def highlight(self, argument):
# Declaration (works)
self.text.highlight_pattern("<!DOCTYPE HTML>", "htmlDeclaration")
self.text.highlight_pattern("<!doctype html>", "htmlDeclaration")
# tags (does not work)
tags = []
with open("html_tags.txt", "r") as taglist:
for tag in taglist:
tags.append(tag)
for i in range(0, len(tags)):
self.text.highlight_pattern(tags[i], "tags")
def createtext(self):
self.text = CustomText(self, bd=0, font=("Courier", 9))
self.text.tag_configure("htmlDeclaration", foreground="#246BB2")
self.text.tag_configure("tags", foreground="#006BB2")
self.text.bind("<Key>", self.highlight)
self.text.pack()
if __name__ == "__main__":
root = tk.Tk()
root.title("Arshi")
window = Arshi(root).pack(side="top", fill="both", expand=True)
root.mainloop()
将tkinter作为tk导入
类CustomText(tk.Text):
定义初始化(self,*args,**kwargs):
tk.Text.\uuuu init\uuuuu(self,*args,**kwargs)
def高亮显示模式(自我、模式、标签、开始=“1.0”、结束=“结束”,
regexp=False):
''将给定标记应用于与给定模式匹配的所有文本
如果“regexp”设置为True,则模式将被视为常规模式
表情。
'''
开始=自索引(开始)
结束=自索引(结束)
自标记集(“匹配开始”,开始)
自标记集(“匹配结束”,开始)
self.mark_集(“searchLimit”,结束)
count=tk.IntVar()
尽管如此:
index=self.search(模式,“匹配结束”,“搜索限制”,
count=count,regexp=regexp)
如果索引==“”:中断
self.mark_集(“匹配开始”,索引)
self.mark_集(“matchEnd”,“%s+%sc%”(index,count.get())
self.tag\u add(tag,“匹配开始”、“匹配结束”)
Arshi类(传统框架):
定义初始化(self,*args,**kwargs):
tk.Frame.\uuuu init\uuuuu(self,*args,**kwargs)
self.createtext()
def突出显示(自身、参数):
#声明(工程)
self.text.highlight_模式(“,“htmlDeclaration”)
self.text.highlight_模式(“,“htmlDeclaration”)
#标签(不起作用)
标签=[]
打开(“html_tags.txt”、“r”)作为标记列表:
对于标记列表中的标记:
tags.append(标记)
对于范围(0,len(标记))中的i:
self.text.highlight_模式(标记[i],“标记”)
def createtext(自):
self.text=CustomText(self,bd=0,font=(“Courier”,9))
self.text.tag_configure(“htmlDeclaration”,前台=“#246BB2”)
self.text.tag_configure(“tags”,前台=“#006BB2”)
self.text.bind(“,self.highlight)
self.text.pack()
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
根标题(“Arshi”)
window=Arshi(root).pack(side=“top”、fill=“both”、expand=True)
root.mainloop()
下面是文本文件(名为html_tags.txt):
其原因是每个标记的末尾也有一个\n
,这就是导致所有内容突出显示的原因
您可以复制类似的问题,方法是将一个简单的标记添加到标记
(而不是通过文件读取),并在末尾添加\n
,如-标记=['\n']
然后,
之后的任何一行都会突出显示
在将每个标记添加到标记
列表之前,应先将其剥离
范例-
将tkinter作为tk导入
类CustomText(tk.Text):
定义初始化(self,*args,**kwargs):
tk.Text.\uuuu init\uuuuu(self,*args,**kwargs)
def高亮显示模式(自我、模式、标签、开始=“1.0”、结束=“结束”,
regexp=False):
''将给定标记应用于与给定标记匹配的所有文本
图案
如果“regexp”设置为True,则模式将被视为
有规律的
表情。
'''
开始=自索引(开始)
结束=自索引(结束)
自标记集(“匹配开始”,开始)
自标记集(“匹配结束”,开始)
self.mark_集(“searchLimit”,结束)
count=tk.IntVar()
尽管如此:
index=self.search(模式,“匹配结束”,“搜索限制”,
count=count,regexp=regexp)
如果索引==“”:中断
self.mark_集(“匹配开始”,索引)
self.mark_集(“matchEnd”,“%s+%sc%”(index,count.get())
self.tag\u add(tag,“匹配开始”、“匹配结束”)
Arshi类(传统框架):
定义初始化(self,*args,**kwargs):
tk.Frame.\uuuu init\uuuuu(self,*args,**kwargs)
self.createtext()
def突出显示(自身、参数):
#声明(工程)
self.text.highlight_模式(“,
“htmlDeclaration”)
self.text.highlight_模式(“,
“htmlDeclaration”)
#标签(不起作用)
标签=[]
打开(“html_tags.txt”、“r”)作为标记列表:
对于标记列表中的标记:
tags.append(tag.strip())
对于范围(0,len(标记))中的i:
self.text.highlight_模式(标记[i],“标记”)
def createtext(自):
self.text=CustomText(self,bd=0,font=(“Courier”,9))
self.text.tag_configure(“htmlDeclaration”,
前景=“#246BB2”)
self.text.tag_configure(“tags”,前台=“#006BB2”)
self.text.bind(“,self.highlight)
self.highlight(无)
self.text.pack()
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
根标题(“Arshi”)
window=Arshi(root).pack(side=“top”、fill=“both”、expand=True)
root.mainloop()
还有一个建议-
您可能希望将标签列表保留在列表中,而不是每次按键都从文件中读回标签列表。您可以将标记列表作为实例变量存储在self
中
此外,如果您在问题中发布的文件已完成,则不会突出显示结束标记(或内联结束标记)。您可能需要将它们单独添加到文件中。非常感谢您抽出时间。这很有魅力。是的,我知道结束标记,我只是简化了我的原始文本编辑器以便于使用。
<a>
<abbr>
<acronym>
<address>
<applet>
<area>
<article>
<aside>
<audio>
<b>
<base>
<basefont>
<bdi>
<bdo>
<big>
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center>
<cite>
<code>
<col>
<colgroup>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir>
<div>
<dl>
<dt>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font>
<footer>
<form>
<frame>
<frameset>
<head>
<header>
<hr>
<html>
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<i>
<iframe>
<img>
<input>
<ins>
<kbd>
<keygen>
<label>
<legend>
<li>
<link>
<main>
<map>
<mark>
<menu>
<menuitem>
<meta>
<meter>
<nav>
<noframes>
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<pre>
<progress>
<q>
<rp>
<rt>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<small>
<source>
<span>
<strike>
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<textarea>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt>
<u>
<ul>
<var>
<video>
<wbr>
import tkinter as tk
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
def highlight_pattern(self, pattern, tag, start="1.0", end="end",
regexp=False):
'''Apply the given tag to all text that matches the given
pattern
If 'regexp' is set to True, pattern will be treated as a
regular
expression.
'''
start = self.index(start)
end = self.index(end)
self.mark_set("matchStart", start)
self.mark_set("matchEnd", start)
self.mark_set("searchLimit", end)
count = tk.IntVar()
while True:
index = self.search(pattern, "matchEnd","searchLimit",
count=count, regexp=regexp)
if index == "": break
self.mark_set("matchStart", index)
self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
self.tag_add(tag, "matchStart", "matchEnd")
class Arshi(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.createtext()
def highlight(self, argument):
# Declaration (works)
self.text.highlight_pattern("<!DOCTYPE HTML>",
"htmlDeclaration")
self.text.highlight_pattern("<!doctype html>",
"htmlDeclaration")
# tags (does not work)
tags = []
with open("html_tags.txt", "r") as taglist:
for tag in taglist:
tags.append(tag.strip())
for i in range(0, len(tags)):
self.text.highlight_pattern(tags[i], "tags")
def createtext(self):
self.text = CustomText(self, bd=0, font=("Courier", 9))
self.text.tag_configure("htmlDeclaration",
foreground="#246BB2")
self.text.tag_configure("tags", foreground="#006BB2")
self.text.bind("<Key>", self.highlight)
self.highlight(None)
self.text.pack()
if __name__ == "__main__":
root = tk.Tk()
root.title("Arshi")
window = Arshi(root).pack(side="top", fill="both", expand=True)
root.mainloop()