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())
由此即可完成简单的编辑器。
Tkinter制作简单的python编辑器的更多相关文章
- python使用wxPython创建一个简单的文本编辑器。
ubuntu下通过'sudo apt-get install python-wxtools'下载wxPython.load和save函数用于加载和保存文件内容,button通过Bind函数绑定这两个函 ...
- python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图
python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...
- python制作简单excel统计报表2之操作excel的模块openpyxl简单用法
python制作简单excel统计报表2之操作excel的模块openpyxl简单用法 # coding=utf-8 from openpyxl import Workbook, load_workb ...
- Python tkinter 实现简单登陆注册 基于B/S三层体系结构,实现用户身份验证
Python tkinter 实现简单登陆注册 最终效果 开始界面 注册 登陆 源码 login.py # encoding=utf-8 from tkinter import * from ...
- python编辑器对比和推荐
python编辑器对比和推荐 我先给一个初步的表格吧,大家如果有什么意见,或有补充,欢迎提出.有些我没有用过,先不写了.以下是我使用过的python IDE: 除了PythonWin, Visua ...
- 转载 - Vim 的 Python 编辑器详细配置过程 (Based on Ubuntu 12.04 LTS)
出处:http://www.cnblogs.com/ifantastic/p/3185665.html Vim 的 Python 编辑器详细配置过程 (Based on Ubuntu 12.04 LT ...
- Unreal Engine 4 系列教程 Part 5:制作简单游戏
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- Unreal Engine 4 系列教程 Part 10:制作简单FPS游戏
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- python教程:用简单的Python编写Web应用程序
python现在已经成为很多程序员关注的编程语言之一,很多程序员也都开始弄python编程,并且很多时候都会用自己的操作来选择,而现在不管是程序员还是少儿编程,都会有python这门课,今天就和大家分 ...
随机推荐
- java基础学习:JavaWeb之Cookie和Session
一.会话概述 1.1.什么是会话? 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话其中不管浏览器发送多少请求,都视为一次会话,直到 ...
- 使用RegSetValueEx修改注册表时遇到的问题(转)
原文转自 http://blog.csdn.net/tracyzhongcf/article/details/4076870 1.今天在使用RegSetValueEx时发现一个问题: RegSetVa ...
- DRM学习总结(1)--- DRM框架介绍
一.DRM 简介 In computing, the Direct Rendering Manager (DRM), a subsystem of the Linux kernel, interfac ...
- aarch64_a2
asterisk-sounds-core-en_GB-1.5.0-2.fc26.noarch.rpm 2017-02-14 08:24 26K fedora Mirroring Project ast ...
- 关于oracle数据库死锁的检查方法
一.数据库死锁的现象程序在执行的过程中,点击确定或保存按钮,程序没有响应,也没有出现报错. 二.死锁的原理当对于数据库某个表的某一列做更新或删除等操作,执行完毕后该条语句不提交,另一条对于这一列数据做 ...
- Python缓存技术,装x新高度。
一段非常简单代码 普通调用方式 def console1(a, b): print("进入函数") return (a, b) print(console1(3, 'a')) pr ...
- python numpy数组中的复制问题
vector = numpy.array([5, 10, 15, 20]) equal_to_ten_or_five = (vector == 10) | (vector == 5) vector[e ...
- 安装mysql5.5的时候出现Error Nr.1045
解决办法: 1.删除注册表几个键值:HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Application\MySQL HKEY_L ...
- scala tuple中的syntactic sugar
List[Tuple2[String, Int]] // Base List[(String, Int)] // Syntactic sugar List[Tuple3[String, Float, ...
- C# TimeSpan获取 年月
public static string GetYearMonthDayString(this DateTime expires) { try { var now = DateTime.Now; Ti ...