• Tkinter制作简单的python编辑器


    想要制作简单的python脚本编辑器,其中文字输入代码部分使用Tkinter中的Text控件即可实现。

    但是问题是,如何实现高亮呢?参考python自带的编辑器:python27/vidle文件夹中的代码。

    实现效果为:

    其中主要思路就是在Text中每输入一行代码,都通过正则匹配,查找是不是需要高亮效果,道理很简单,但是问题就是在使用Text控件的时候,通过光标输入的时候,似乎不能找到光标对应的位置,所以,人家的编辑器代码中,提供了WidgetRedirector.py文件,其作用主要是解决控件的输入操作执行Tk库里面的insert,而跳过了Tkinter库对应Text类中的insert函数。

    该类的作用就是使用register函数注册insert的函数funtion,当往Text输入时,调用了funtion,然后从这个funtion中,即可得到文字输入的位置,而原始的insert函数中,往Text书写的操作,是通过该文件中的OriginalCommand类实现的。

    其中的

    WidgetRedirector类和OriginalCommand类直接拷贝即可。

    而颜色高亮主要在ColorDelegator.py文件中实现,可以使用其中的正则表达式。

    实现Text高亮的部分为:

    class Test(object):
        def __init__(self,parent):
            self.parent = parent
            self.text = Text(self.parent)
            self.text.pack()
            self.text.focus_set()
            self.redir = WidgetRedirector(self.text)
            self.redir.insert  = self.redir.register("insert", self.m_insert)
            self.redir.delete  = self.redir.register("delete", self.m_delete)
            self.prog = prog
            
            self.tagdefs = {'COMMENT': {'foreground': '#dd0000', 'background': '#ffffff'}, 'DEFINITION': {'foreground': '#0000ff', 'background': '#ffffff'}, 'BUILTIN': {'foreground': '#900090', 'background': '#ffffff'}, 'hit': {'foreground': '#ffffff', 'background': '#000000'}, 'STRING': {'foreground': '#00aa00', 'background': '#ffffff'}, 'KEYWORD': {'foreground': '#ff7700', 'background': '#ffffff'}, 'ERROR': {'foreground': '#000000', 'background': '#ff7777'}, 'TODO': {'foreground': None, 'background': None}, 'SYNC': {'foreground': None, 'background': None}, 'BREAK': {'foreground': 'black', 'background': '#ffff55'}}
            
            for tag, cnf in self.tagdefs.items():
                if cnf:
                    self.text.tag_configure(tag, **cnf)
                    
    
        def m_delete(self, index1, index2=None):
            index1 = self.text.index(index1)
            self.redir.delete(index1, index2)  
            self.notify_range(index1,index1)
            
        def m_insert(self, index, chars, *args):
            index = self.text.index(index)   
            self.redir.insert(index, chars, *args)       
            self.notify_range(index, index + "+%dc" % len(chars))
            
        def notify_range(self, index1, index2=None):
            first = index1[0]+'.0'
            line = self.text.get(first, index2)
            
            for tag in self.tagdefs.keys():
                self.text.tag_remove(tag, first, index2)
            chars = line
            m = self.prog.search(chars)
            
            while m:
                for key, value in m.groupdict().items():
                    if value:
                        a, b = m.span(key)
                        self.text.tag_add(key,
                                     first + "+%dc" % a,
                                     first + "+%dc" % b)
                        
                m = self.prog.search(chars, m.end())
    

     由此即可完成简单的编辑器。

    作者:禅在心中

    出处:http://www.cnblogs.com/pinking/

    本文版权归作者和博客园共有,欢迎批评指正及转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    写在vue总结之前(一)
    前端应该掌握的web基础和网络知识
    sass之为什么要使用预处理器
    ThinkPHP简单的验证码实现
    ajax接收php返回得到一堆html代码
    Bootstrap 4,“未捕获错误:Bootstrap工具提示需要Tether(http://github.hubspot.com/tether/)”
    百度AI开放平台- API实战调用
    最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)
    C#避免踩坑之如何添加paint事件
    php插入mysql中文数据出现乱码
  • 原文地址:https://www.cnblogs.com/pinking/p/6817116.html
Copyright © 2020-2023  润新知