使用YAML(文本编辑器)突出显示Python语法

时间:2018-10-20 05:47:23

标签: python-3.x yaml

我正在使用Tkinter模块在python中创建一个简单的文本编辑器。最近,我想在我的文本编辑器中添加语法突出显示功能。我引用了一些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'

下面是代码段
代码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'

再次感谢!

1 个答案:

答案 0 :(得分:1)

您的yaml文件存在语法错误,但这不是问题。修复语法错误后,您首先需要这样做:

config = yaml.load(stream)

config现在是具有单个键“类别”的映射(字典),并且是具有单个键“关键字”的映射。

稍后您这样做:

self.numbers_color = config['number']['color']

config没有密钥'number',所以您遇到密钥错误。 config拥有的唯一密钥是categories,其下的唯一关键字是“关键字”。

如果您要获取“绿色”值,则需要执行以下操作:

# get the 'categories' mapping
self.categories = config['categories']

# from 'categories', get the 'Keywords' mapping
self.keywords = self.categories['Keywords']

# from the 'Keywords' mapping, get the 'color' value for 'number'
self.numbers_color = self.keywords['number']['color']