idlelib tkinter语法突出显示重置/更新/删除

发布于 2025-01-20 12:11:27 字数 2115 浏览 3 评论 0原文

我正在制作一个使用idlelib percolator 和 colordelegator 进行语法突出显示的IDE。在我的特殊文本小部件 init 函数中,它运行:

import idlelib.colorizer as ic
import idlelib.percolator as ip
        self.cdg = ic.ColorDelegator()
        self.cdg.prog = re.compile(r"\b(?P<MYGROUP>tkinter)\b|" + ic.make_pat(), re.S)
        self.cdg.idprog = re.compile(r"\s+(\w+)", re.S)
        self.cdg.tagdefs["MYGROUP"] = {"foreground": tag_mygroup, "background": "#FFFFFF"}
        self.cdg.tagdefs["COMMENT"] = {"foreground": tag_comment, "background": "#FFFFFF"}
        self.cdg.tagdefs["KEYWORD"] = {"foreground": tag_keyword, "background": "#FFFFFF"}
        self.cdg.tagdefs["BUILTIN"] = {"foreground": tag_builtin, "background": "#FFFFFF"}
        self.cdg.tagdefs["STRING"] = {"foreground": tag_string, "background": "#FFFFFF"}
        self.cdg.tagdefs["DEFINITION"] = {"foreground": tag_definition, "background": "#FFFFFF"}
        self.cdg.tagdefs["CLASS"] = {"foreground": tag_class, "background": "#FFFFFF"}
        ip.Percolator(self.text).insertfilter(self.cdg)

这使文本小部件语法按预期突出显示。我想允许用户更改设置文件中的颜色代码,然后重置/更新或删除,然后重新应用使用新颜色突出显示的语法。问题是,当我尝试使用 ip.Percolator(self.text).removefilter(self.cdg) 删除突出显示时,它会给出错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/Users/joshyacky/Documents/PyApps/Working/JDE/JDE.py", line 129, in reset_syntax_colors
    main_text_box.reset_colors()
  File "/Users/joshyacky/Documents/PyApps/Working/JDE/custom_widgets.py", line 95, in reset_colors
    ip.Percolator(self.text).removefilter(self.cdg)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/idlelib/percolator.py", line 10, in __init__
    self.redir = WidgetRedirector(text)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/idlelib/redirector.py", line 44, in __init__
    tk.call("rename", w, self.orig)
_tkinter.TclError: can't rename to ".!ultra_text.!text_orig": command already exists

有关如何删除或更新语法突出显示的任何想法?

I am making an IDE that uses the idlelib percolator and colordelegator for syntax highlighting. In my special text widgets init function it runs:

import idlelib.colorizer as ic
import idlelib.percolator as ip
        self.cdg = ic.ColorDelegator()
        self.cdg.prog = re.compile(r"\b(?P<MYGROUP>tkinter)\b|" + ic.make_pat(), re.S)
        self.cdg.idprog = re.compile(r"\s+(\w+)", re.S)
        self.cdg.tagdefs["MYGROUP"] = {"foreground": tag_mygroup, "background": "#FFFFFF"}
        self.cdg.tagdefs["COMMENT"] = {"foreground": tag_comment, "background": "#FFFFFF"}
        self.cdg.tagdefs["KEYWORD"] = {"foreground": tag_keyword, "background": "#FFFFFF"}
        self.cdg.tagdefs["BUILTIN"] = {"foreground": tag_builtin, "background": "#FFFFFF"}
        self.cdg.tagdefs["STRING"] = {"foreground": tag_string, "background": "#FFFFFF"}
        self.cdg.tagdefs["DEFINITION"] = {"foreground": tag_definition, "background": "#FFFFFF"}
        self.cdg.tagdefs["CLASS"] = {"foreground": tag_class, "background": "#FFFFFF"}
        ip.Percolator(self.text).insertfilter(self.cdg)

This gives the text widget syntax highlighting as it is supposed to. I want to allow the user to change the color codes in the settings file and then reset/update or remove and then reapply the syntax highlighting with the new colors. The problem is that when I try to remove the highlighting with ip.Percolator(self.text).removefilter(self.cdg) it gives the error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/Users/joshyacky/Documents/PyApps/Working/JDE/JDE.py", line 129, in reset_syntax_colors
    main_text_box.reset_colors()
  File "/Users/joshyacky/Documents/PyApps/Working/JDE/custom_widgets.py", line 95, in reset_colors
    ip.Percolator(self.text).removefilter(self.cdg)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/idlelib/percolator.py", line 10, in __init__
    self.redir = WidgetRedirector(text)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/idlelib/redirector.py", line 44, in __init__
    tk.call("rename", w, self.orig)
_tkinter.TclError: can't rename to ".!ultra_text.!text_orig": command already exists

Any ideas on how to remove or update the syntax highlighting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-27 12:11:27

我建议看看 IDLE 如何更新颜色。当单击或选择设置对话框上的 [Ok] 时,每个编辑器窗口上都会调用 window.ResetColorizer。这是在idlelib/editor.py第797行中定义的。它又调用前面几行定义的_rmcolorizer和_addcolorizer。 (该函数的其余部分是 IDLE 特定的。) _rmcolorizer 首先在 per.removefilter 之前调用现有的 ColorDelegator().removecolors。此后,_addcolorizer 在调用 per.insertfilter 之前创建一个新的 ColorDelegator()。我不知道这一切是否绝对需要,但它确实有效。

您正在使用几乎未记录的私有代码。不支持第三方使用。不过,我愿意接受建议,例如这些模块的更好文档,这将(也)有助于它们在 IDLE 中的使用。

I suggest looking at how IDLE updates colors. When one clicks or selects [Ok] on the settings dialog, window.ResetColorizer is called on each editor window. That is defined in idlelib/editor.py at line 797. That in turn calls _rmcolorizer and _addcolorizer, defined on preceding lines. (The rest of the function is IDLE specific.) _rmcolorizer first calls the existing ColorDelegator().removecolors before per.removefilter. _addcolorizer thereafter creates a new ColorDelegator() before calling per.insertfilter. I don't know if all this is absolutely needed, but it works.

You are using nearly undocumented private code. It is not supported for 3rd party uses. However, I am open to suggestions, such as better doc for these modules, that would (also) aid their use for IDLE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文