Я создаю простой текстовый редактор на python, используя модуль Tkinter.Недавно я хотел добавить функцию подсветки синтаксиса в мой текстовый редактор.Я ссылаюсь на PDF и создаю этот кусок кода, но у меня возникают некоторые проблемы.Спасибо за помощь
Это следующая ошибка:
imtiyaz@Baka:~/Dropbox/NoteBooktest/test$ python3 high.py
Traceback (most recent call last):
File "high.py", line 48, in <module>
hello(root,'yamlsh.yml')
File "high.py", line 33, in __init__
self.parse_syntax_file()
File "high.py", line 13, in parse_syntax_file
self.numbers_color = config['number']['color']
KeyError: 'number'
Ниже приведен фрагмент кода
code high.py
from tkinter import *
import yaml
class hello:
def parse_syntax_file(self):
with open(self.syntax_file,'r') as stream:
try:
config = yaml.load(stream)
except yaml.YAMLError as error:
print(error)
return
self.categories = config['categories']
self.numbers_color = config['number']['color']
self.strings_color = config['string']['color']
self.configure_tag()
def configure_tag(self):
for category in self.categories.keys():
self.color = self.categories['category']['color']
self.text_waidget.tag_configure(category, foreground=self.color)
self.text_widget.tag_configure("number", foreground=self.numbers_color)
self.text_widget.tag_configure("string", foreground=self.strings_color)
def __init__(self,master, syntax_file):
self.master = master
self.syntax_file = syntax_file
self.text_widget = Text(self.master)
self.text_widget.pack()
self.categories = None
self.numbers_color = 'green'
self.strings_color = 'red'
self.disallow_pre_char = ["_","-",",","."]
self.parse_syntax_file()
self.text_widget.bind('<KeyRelease>',self.on_key_release)
def on_key_release(self, event=None):
self.highlight()
def highlight(self, event=None):
length = IntVar()
for category in self.categories:
matche = self.categories[category]['matche']
for keyword in matche:
start = 1.0
self.text_widget.tag_add(category,idx,end)
root = Tk()
hello(root,'yamlsh.yml')
root.mainloop()`
yamlsh.yml
categories:
Keywords:
color:orange
matche:[for, def, while, from, import, as, with, self]
variables:
color: red4
matche: ['True', 'False', 'None']
conditionals:
color: green
matche: [try, except, if, else, elif]
functions:
color: blue4
matche: [int,str,dict,list,set,float]
number:
color: green;
string:
color: '#e1218b'
Еще раз спасибо!