Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tkinter - Treeview - .selection_add and selection_toggle #70574

Closed
gbarnabic mannequin opened this issue Feb 18, 2016 · 8 comments
Closed

tkinter - Treeview - .selection_add and selection_toggle #70574

gbarnabic mannequin opened this issue Feb 18, 2016 · 8 comments
Assignees
Labels
topic-tkinter type-bug An unexpected behavior, bug, or error

Comments

@gbarnabic
Copy link
Mannequin

gbarnabic mannequin commented Feb 18, 2016

BPO 26386
Nosy @terryjreedy, @ned-deily, @serhiy-storchaka
PRs
  • [2.7] bpo-26386: Re-enable missing widget testcases in test_ttk_guionly. #12014
  • Files
  • ttk_treeview_selection.patch
  • 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:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2016-06-14.17:42:33.287>
    created_at = <Date 2016-02-18.16:30:08.197>
    labels = ['type-bug', 'expert-tkinter']
    title = 'tkinter - Treeview - .selection_add and selection_toggle'
    updated_at = <Date 2019-02-24.08:07:19.824>
    user = 'https://bugs.python.org/gbarnabic'

    bugs.python.org fields:

    activity = <Date 2019-02-24.08:07:19.824>
    actor = 'ned.deily'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2016-06-14.17:42:33.287>
    closer = 'serhiy.storchaka'
    components = ['Tkinter']
    creation = <Date 2016-02-18.16:30:08.197>
    creator = 'gbarnabic'
    dependencies = []
    files = ['42018']
    hgrepos = []
    issue_num = 26386
    keywords = ['patch']
    message_count = 8.0
    messages = ['260469', '260553', '260780', '268470', '268547', '268575', '336446', '336448']
    nosy_count = 5.0
    nosy_names = ['terry.reedy', 'ned.deily', 'python-dev', 'serhiy.storchaka', 'gbarnabic']
    pr_nums = ['12014']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26386'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

    @gbarnabic
    Copy link
    Mannequin Author

    gbarnabic mannequin commented Feb 18, 2016

    Id's with spaces in them causes a crash when using the .selection* methods Treeview. Version of ttk.py "0.3.1" dated 12/6/2015. Traceback line numbers are 1415 then 1395

    Either of these lines of code, where the item id is "2009 Report.pdf" crash
    allParents = oTree.get_children()
    for id in allParents:
    oTree.selection_add(id)
    # oTree.selection_toggle(id)

    These two lines of workaround code do work however.
    oTree.selection_add('"' + id + '"')
    # oTree.selection_toggle('"' + id + '"')

    Note that so far all other places in dealing with the item id's have no issue when there are spaces in them.

    @gbarnabic gbarnabic mannequin added type-crash A hard crash of the interpreter, possibly with a core dump topic-tkinter labels Feb 18, 2016
    @terryjreedy
    Copy link
    Member

    The 3.5 version of 2015 Dec 6 is 3.5.1. A traceback is not a crash for the purpose of this tracker.

    It is generally a good idea to mention OS, OS version, and Tk Version. (IDLE displays TkVersion in Help => About IDLE.) However, on Win 10 (8.6.4), I was able to reproduce with all four of the .selection_xyz(items) methods, which are all specializations of the .selection(op, items) methods.

    import tkinter as tk
    from tkinter import ttk
    root = tk.Tk()
    tree = ttk.Treeview(root)
    tree.pack()
    tree.insert('', 1, iid='a b', text='id with space')
    for item in tree.get_children():
        print(item)
        #tree.selection_add(item)  # fail
        #tree.selection_toggle(item)  # fail
        #tree.selection_set(item)  # fail
        #tree.selection_remove(item)  # fail
        tree.selection('add', item)  # fail

    getting

    a b
    Traceback (most recent call last):
      File "F:\Python\mypy\tem.py", line 10, in <module>
        tree.selection_toggle(item)
      File "C:\Programs\Python35\lib\tkinter\ttk.py", line 1415, in selection_toggle
        self.selection("toggle", items)
      File "C:\Programs\Python35\lib\tkinter\ttk.py", line 1395, in selection
        return self.tk.call(self._w, "selection", selop, items)
    _tkinter.TclError: Item a not found

    tkinter

    These are the only Treeview methods in which a single argument can be multiple items. The doc string and doc do not define the form of 'items'. The unofficial but generally helpful doc, http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Treeview.html says "The argument may be either a single iid or a sequence of iids." I would expect sequence mean a tuple of strings, not space-separated fields in a string (a tcl sequence).

    The .delete and .detach methods have signature '*items', meaning that each item is passed as a separate argument. The methods them pass the tuple of strings to tk. This should have been the signature of the selection methods.

    Putting a single string in a tuple solves the problem, as in.

        tree.selection('add', (item,))

    My suggested fix is to add a statement to .selection

        def selection(self, selop=None, items=None):
            """If selop is not specified, returns selected items."""
            if isinstance(items, str):  # new
                items = (items,)
            return self.tk.call(self._w, "selection", selop, items)

    @terryjreedy terryjreedy added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Feb 20, 2016
    @serhiy-storchaka
    Copy link
    Member

    Here is the patch with tests.

    But I like the idea to make the selection_*() methods conforming with the delete() and detach() methods in 3.6.

    @terryjreedy
    Copy link
    Member

    LGTM, but after applying to all 3, leave open (or open new issue) to replace patch in 3.6 (before beta 1).

    Change signature to *items. That will automatically make a single string become a len 1 tuple. For selection (list the 4 selops and) add a note. "For back compatibility, one may also pass a single tuple (or list?) of items." and Version Changed. For 4 derivatives, 'See selection.' In the code, instead add (untested):

        if len(items) == 1:  # Remove after deprecation.
            item0 = items[0]
            if not isinstance(items[0], isinstance(str)):
                raise DeprecationWarning(
                     <level arg>
                     "The option of passing multiple items as a tuple "
                     "rather than as multiple items is deprecated and "
                     "will be removed in 2020 or later.")
                items = items0  # or more processing if lists were allowed

    Revise test accordingly.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 14, 2016

    New changeset 0885477fecfd by Serhiy Storchaka in branch '3.5':
    Issue bpo-26386: Fixed ttk.TreeView selection operations with item id's
    https://hg.python.org/cpython/rev/0885477fecfd

    New changeset d1c0bc1d0625 by Serhiy Storchaka in branch 'default':
    Issue bpo-26386: Fixed ttk.TreeView selection operations with item id's
    https://hg.python.org/cpython/rev/d1c0bc1d0625

    New changeset ac7c10f5e36e by Serhiy Storchaka in branch '2.7':
    Issue bpo-26386: Fixed ttk.TreeView selection operations with item id's
    https://hg.python.org/cpython/rev/ac7c10f5e36e

    @serhiy-storchaka
    Copy link
    Member

    Thank you Terry.

    Opened bpo-27319 for changing the signature of selection methods.

    @ned-deily
    Copy link
    Member

    New changeset 72ff2f7 by Ned Deily in branch '2.7':
    bpo-26386: Re-enable missing widget testcases in test_ttk_guionly. (GH-12014)
    72ff2f7

    @ned-deily
    Copy link
    Member

    It looks like some testing code was inadvertently committed in the 2.7 branch fix causing only the Treeview testcases to be executed when test_ttk_gui_only is run. (I noticed this because a testcase was failing on other branches but not 2.7 although the tests are virtually identical.) PR 12014 fixes this.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-tkinter type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants