【问题标题】:Python syntax highlighting with the YAML(text editor)使用 YAML(文本编辑器)突出显示 Python 语法
【发布时间】:2018-10-20 05:47:23
【问题描述】:

我正在使用 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'

下面是代码sn-p
代码高.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'

再次感谢!

【问题讨论】:

  • 给定的代码和 YAML 文件不应产生您显示的错误。确保 YAML 格式正确(粘贴时缩进可能出错)。另外,请创建一个MVCE,这样您的问题就很明显了。这通常也有助于您更好地理解问题。现在,这看起来更像是请求代码审查,而不是询问特定问题。
  • @flyx 正如你所说,我已经在我的 .yml 文件中进行了更改,但仍然存在相同的错误
  • @AFTABKHAN 您的代码表明您在 Unix/Linux/macOS 上,您使用的是什么文件系统,您无法使用推荐的 .yaml 扩展名 (yaml.org/faq.html)?
  • @Anthon:后缀无关紧要。

标签: python-3.x yaml


【解决方案1】:

您的 yaml 文件有语法错误,但这不是问题所在。一旦你修复了语法错误,你首先要这样做:

config = yaml.load(stream)

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

稍后你会这样做:

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

config 没有密钥“数字”,因此您收到密钥错误。 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']

【讨论】:

  • 对不起,但我还是没明白如何检查我的 .yaml 文件错误
猜你喜欢
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
相关资源
最近更新 更多