python / cpython

The Python programming language
https://www.python.org
Other
62.3k stars 29.93k forks source link

Document default tk Text class bindings for tkinter and IDLE #70273

Open c0ec9cda-1d66-4b3b-8ab3-1aa6c6d34784 opened 8 years ago

c0ec9cda-1d66-4b3b-8ab3-1aa6c6d34784 commented 8 years ago
BPO 26085
Nosy @terryjreedy, @serhiy-storchaka
Files
  • 2016-01-11--1452545267_1920x1080_scrot.png: My code and results
  • tem.py: Illustrate bug and fix
  • tem.tcl
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = 'https://github.com/terryjreedy' closed_at = None created_at = labels = ['3.7', 'type-bug', 'expert-tkinter', 'docs'] title = 'Document default tk Text class bindings for tkinter and IDLE' updated_at = user = 'https://bugs.python.org/freshnick' ``` bugs.python.org fields: ```python activity = actor = 'terry.reedy' assignee = 'terry.reedy' closed = False closed_date = None closer = None components = ['Documentation', 'Tkinter'] creation = creator = 'fresh_nick' dependencies = [] files = ['41585', '41631', '41632'] hgrepos = [] issue_num = 26085 keywords = [] message_count = 5.0 messages = ['258019', '258379', '258383', '279336', '279365'] nosy_count = 4.0 nosy_names = ['terry.reedy', 'docs@python', 'serhiy.storchaka', 'fresh_nick'] pr_nums = [] priority = 'normal' resolution = None stage = 'needs patch' status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue26085' versions = ['Python 3.5', 'Python 3.6', 'Python 3.7'] ```

    c0ec9cda-1d66-4b3b-8ab3-1aa6c6d34784 commented 8 years ago

    Hello, I have Python 3.4.3 and Tk/Tcl 8.5 (built in Python; reported by tk.TclVersion and tk.TkVersion). When I assign printing 'This works' to a hotkey, the program prints 'This worsk'. After pressing the hotkey again, 'worsk' is replaced with 'works', but when the hotkey is pressed repeatedly, we can see 'worsk' sometimes (see the attachment).

    Have a nice day.

    terryjreedy commented 8 years ago

    You should have uploaded your .py file. Mine is attached, with fix added. Anyway, confirmed with 3.5.1, Win10. Slightly more bizarre than described, in that misspelling disappears on even insertion and reappears in odd insertion.

    this worsk this worksthis works this worksthis worksthis worsk

    Even stranger, printing the txt to Shell after the insertion shows the opposite parity.

    this works this worksthis worsk this worksthis worksthis works

    Binding to another key removes the problem. By experiment, ^t in Text means 'transpose chars to left and right of cursor and move cursor right' or equivalently, 'move char to right before the one on left'. So 'abc|def' becomes 'abdc|ef' (where '|' here represents the cursor, not a character. The exception is that at the end of the text, the two chars to the left are transposed, so that 'abc|' becomes 'acb|'.

    Tk.bind has an 'add' parameter that defaults to None, which should mean 'replace previous bindings'. But I suspect that this only applies to user-added bindings, so not replacing a default Text binding is not considered a bug.

    Serhiy, am I correct here or is this a tk or tkinter bug?

    In fact, it seems that this default binding cannot be unbound. Adding 'txt.unbind('\<Control-c>') did not work. Adding "return 'break'" at the end of the callback does work.

    Bottom line: make sure that you bind to an unused sequence or add the return to disable any unused binding you do not know about.

    Our doc is deficient, but correcting that is beyond the scope of this issue. Unless I have missed something, this should be closed as 'not a bug'.

    serhiy-storchaka commented 8 years ago

    I can't reproduce the result with tem.py. If change binding to \<Control-t> and comment out the line "return 'break'" in the callback I can reproduce it.

    This is expected and documented (but only in bind() docstring) behavior. If the callback returns 'break', no other functions (including standard handlers) will be invoked for the event. Otherwise the standard handler for \<Control-t> is invoked and it swaps two characters as Terry said.

    If doesn't consider this issue as the documentation issue, it should be closed as not a bug.

    Here is Terry's example translated to pure Tcl.

    serhiy-storchaka commented 7 years ago

    Does anybody want to provide documentation patch? Otherwise this issue will be closed as "not a bug".

    terryjreedy commented 7 years ago

    I want to recast this as a doc issue. The BINDINGS section of http://www.tcl.tk/man/tcl8.6/TkCmd/text.htm, near the bottom, has a list with 33 numbered items. The main categories of action include selection modification, cursor movement, and text deletion. Minor categories include insertion, transposition, undo/redo. However, the items are not neatly grouped this way.

    Some of these actions are IDLE menu items and are listed in the IDLE menu doc. Many more are documented in https://docs.python.org/3/library/idle.html#editing-and-navigation. Some useful actions, such as ^t Tranposition Right, are omitted.

    Some of this IDLE doc is wrong, at least for IDLE, at least on Windows. For instance, at least on Windows, ^a in IDLE is Select All, not Move Beginning of Line.

    For working on IDLE, it would be very helpful to have a *categorized* listing of class-bound actions that are verified to work for tkinter Text, with notes on any OS differences. I can then check what works on IDLE and how it changes the bindings on either some or all systems. I can and someday will do this for Windows, but I currently would need help for other OSes.

    To help people avoid clashes such as Nick ran into, the tkinter doc should also have a sorted and abbreviated list of bound event sequences. "Text comes with bindings for the following event sequences for keys: Control-a, ..., z; Control-Shift-?, ...,z, Meta-...(Unix), Command-...(Mac). For mice, ..." Follow with a note on what can or cannot be unbound and the need to use 'break' when overriding.

    A function to produce for IDLE a similar list that includes IDLE's system-specific add-ons and a user's customizations, would be a separate but very useful issue. Not knowing current bindings makes customization hard. Another spinoff issue would be making actions available via the IDLE menu system.