Editing text with an external editor in Python

Terry Reedy tjreedy at udel.edu
Wed Sep 3 03:49:25 CEST 2014


On 9/2/2014 5:36 PM, Chris Angelico wrote:
> On Wed, Sep 3, 2014 at 7:14 AM, Terry Reedy <tjreedy at udel.edu> wrote:
>> import tkinter as tk
>> root = tk.Tk()
>> text = tk.Text()
>> text.pack()
>> root.mainloop()
>>
>> I tested tested the functions and wrote the following.
>>
>> This is a test text entry.
>> Enter and Tab work as expected.
>> The Arrow (Cursor) keys work as expected.
>> CntL-Left and Cntl-Right move a word at time.
>> Home and End move to beginning and end of the line.
>> Cntl-Home and Cntl-Up move to the beginning of the text.
>> Cntl-End and Cntl-Donw move to the end of the text.
>> Shift + cursor movement selects between the begin and end slice positions.
>> PageUp and PageDown are inoperative.

My mistake. See below.

>> Delete and Backspace work as expected.
>> At least on Windows, I can select text and delete,
>> or cut or copy to Clipboard.
>> I can also paste from the clipboard.
>> In otherwords, this is a functional minimal text entry widget.

'Minimal' mean minimal, as in the minimal number of lines of code to 
actually work (4), as a the minimum needed to prove the concept, and a 
starting point for adding more things.

> Err, that's not all it takes to run an editor :) File opening

The opening premise of the thread was something like "Input() is good 
for getting a single line of text input from a user. What about getting 
multiple lines of text?"  Opening a non-empty existing file is not 
relevant to the purpose of extending input().  I intentionally said 
'text entry widget', not 'editor'.  For editing a particular existing 
text, the app might pre-load the widget with that text.

> saving would be kinda helpful, for a start...

It is really annoying when someone clips (erases) something I said and 
then says that I should have said what I did say.  Here it is again!

 >> In use, a save function would have to be added.

The function does not have to be attached to menu widget.  When one 
imports a patch into a mercurial repository, hg offers the opportunity 
to enter a multi-line commit message. Just the scenario this thread 
started with. It opens a text entry window something like tkinter.Text. 
It is preloaded with something like the following.

-----------------------------------X|
|                                   |
|===================================|
|Enter commit message above the line|
|or leave blank to abort commit.    |
|Close window when done.            |
-------------------------------------

Hg intercepts the window close event to grab the entered text, if any, 
before it disappears.

> But that's what I meant when I said that a multi-line entry field is
> an extremely standard widget. (I'm a bit surprised that PgUp/PgDn
> don't work,

My mistake. They do when I add more lines than fit in the window.

-- 
Terry Jan Reedy




More information about the Python-list mailing list