Tk Source Code

View Ticket
Login
Ticket UUID: b8fcb26529202525bfe437ca0a4216eec914aae3
Title: some inconsistencies in text entry widgets and text
Type: Bug Version: 8.6
Submitter: anonymous Created on: 2023-09-12 20:36:49
Subsystem: 07. [entry] Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2023-09-17 19:03:52
Resolution: None Closed By: nobody
    Closed on:
Description:
Each of the 3 texting widgets,

entry
ttk::entry
text

say in the manual:

"If any normal printing characters are typed (in an entry), they are inserted at the point of the insertion cursor."


However, this is not always the case. The below 5 example programs demonstrate that when the program creates a selection, moves the insertion cursor outside the selection (or just leaves it where it was), and gives focus to the widget, typed characters (e.g. 123) will sometimes replace the selection, and sometimes not. If it replaces the selection, the inserted characters are not where the cursor was.

Note that interactive selections (user does the selection, not the program) don't apply since the cursor will always end up adjacent to the selection. I believe this is why this behavior may not have been noticed before.

The reason for these behaviors can be seen in the binding for <Key> which results in calls to several different procs.

Note that more specific bindings, such as <Key-BackSpace> take precedence and also have inconsistent results. The 2 entry's will delete the selection, but in ttk::entry the cursor is also moved. In text, it will delete 1 char at a time until it gets to the selection. Then it deletes the entire selection. However, the manual for text says:

"Backspace and Control-h delete the selection, if there is one in the widget. If there is no selection, they delete the character to the left of the insertion cursor."

And in addition, Control-h never deletes the full selection only 1 character at a time.

Examples (run each separately - I paste them into a windows console or rlwrap of wish):

ttk::entry .te -textvariable tevar ;# will delete selection
pack .te
set tevar "abc def ghi"
.te selection range 0 3
.te icursor end
focus -force .te

entry .te -textvariable tevar ;# will not delete selection
pack .te
set tevar "abc def ghi"
.te selection range 0 3
.te icursor end
focus -force .te

entry .te -textvariable tevar ;# will delete selection
pack .te
set tevar "abc def ghi"
.te selection range 0 3
.te icursor 0
focus -force .te

text .t ;# will not delete selection
pack .t
.t insert 0.0 "abc def ghi"
.t tag add sel 1.0 1.3
.t mark set insert end
focus -force .t

text .t ;# will delete selection
pack .t
.t insert 0.0 "abc def ghi"
.t tag add sel 1.0 1.3
.t mark set insert 1.3
focus -force .t
User Comments: fvogel added on 2023-09-17 19:03:52:

Let's discuss insertion of so-called "normal printing characters" first.

Analysis of the code bound to the <Key> event shows the behavior of each widget is intentional. The (Tk) entry and text widget both have the following comment for the bound proc:

# Insert a string into a text at the point of the insertion cursor.
# If there is a selection in the text, and it covers the point of the
# insertion cursor, then delete the selection before inserting.

Of course what matters here, and is not reflected in the documentation, is the part between the commas.

In contrast, the ttk::entry widget unconditionally deletes the selection before inserting the text:

#	If a selection is present, the new text replaces it.
#	Otherwise, the new text is inserted at the insert cursor.

So, in short, the reported behavior seems intentional. Is it deemed to be wrong? Or should the documentation rather be more precise?