Tkinter making a simple Python editor

Source: Internet
Author: User
Tags python script

You want to make a simple Python script editor where the text input Code section is implemented using the text control in Tkinter.

But the question is, how do you achieve highlighting? Refer to Python's own editor: code in the Python27/vidle folder.

The implementation effect Is:

One of the main ideas is that in text every input line of code, all through regular matching, to find whether it is necessary to highlight the effect, the reason is very simple, but the problem is that when using the text control, through the cursor input, it seems that the cursor can not find the corresponding position, so, People's Editor code, Provides the widgetredirector.py file, which is mainly to solve the input operation of the control to execute the insert inside the TK library, and skips the Tkinter library corresponding to the Insert function in the text class.

The function of this class is to register the Insert function with the Register function funtion, when the text input, call the funtion, and then from this funtion, you can get the position of the text input, and the original insert function, the operation of writing to text, is implemented by the Originalcommand class in the File.

One of the

The Widgetredirector class and the Originalcommand class can be copied directly.

Color highlighting is mainly implemented in the colordelegator.py file, which can be used with regular expressions.

The sections that implement text highlighting are:

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 '}, ' definit ION ': {' 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 ': {' F Oreground ': ' black ', ' background ': ' #ffff55 '}} for tags, cnf in Self.tagdefs.items (): if Cnf:self.text.tag_configur E (tag, **cnf) def m_delete (self, index1, index2=none): index1 = Self.text.index (index1) s Elf.redir.delete (index1, index2) self.notify_range (index1,index1) def m_insert (self, index, chars, *ar gs): index = self.text.index (index) self.redir.insert (index, chars, *args) Self.notify_rang E (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_remov E (tag, first, index2) chars = line m = Self.prog.search (chars) while m:for key, v Alue 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 ())

This allows you to complete a simple editor.

Tkinter making a simple Python editor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.