Python Forum
[Tkinter] About Tkinter Treeview.selection_get() usage.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] About Tkinter Treeview.selection_get() usage.
Tkinter Treeview widget has selection_get, but document not introduce about that, how to use it?

from tkinter import Tk, ttk
root_ = Tk()
tree1 = ttk.Treeview(root_, height = 10, selectmode = 'browse')
tree1.insert('', 'end', text = 'item 0')
tree1.insert('', 'end', text = 'item 1')
tree1.insert('', 'end', text = 'item 2')
tree1.insert('', 'end', text = 'item 3')

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Program Files\Python310\lib\tkinter\", line 1007, in selection_get return'selection', 'get') + self._options(kw)) _tkinter.TclError: PRIMARY selection doesn't exist or form "STRING" not defined
If you are wanting to get the values of selected row, maybe this will help get you started.

#! /usr/bin/env python3

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

class Window:
    def __init__(self, parent):
        columns = ('Column 1', 'Column 2', 'Column 3', 'Column 4')
        self.tree = ttk.Treeview(parent, columns=columns, show='headings', selectmode='browse')
        for column in columns:
            self.tree.heading(column, text=column)

        data = []
        for i in range(1, 50):
            data.append((f'Column 1 Data {i}', f'Column 2 Data {i}', f'Column 3 Data {i}', f'Column 4 Data {i}'))

        for info in data:
            self.tree.insert('', tk.END, values=info)
        self.tree.grid(column=0, row=0, sticky='news')

        scrollbar = ttk.Scrollbar(parent, command=self.tree.yview)
        scrollbar.grid(column=4, row=0, sticky='ns')
        self.tree.bind('<ButtonRelease-1>', self.selectItem)

    def selectItem(self, event):
        current_item = self.tree.focus()
        messagebox.showinfo('Current Selection', f'{self.tree.item(current_item)}')

def main():
    root = tk.Tk()

BashBedlam likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player

It is inherited from tk.XView. From the code:
Lib/tkinter/ Wrote:
    def selection_get(self, **kw):
        """Return the contents of the current X selection.
        A keyword parameter selection specifies the name of
        the selection and defaults to PRIMARY.  A keyword
        parameter displayof specifies a widget on the display
        to use. A keyword parameter type specifies the form of data to be
        fetched, defaulting to STRING except on X11, where UTF8_STRING is tried
        before STRING."""
        if 'displayof' not in kw: kw['displayof'] = self._w
        if 'type' not in kw and self._windowingsystem == 'x11':
                kw['type'] = 'UTF8_STRING'
                return'selection', 'get') + self._options(kw))
            except TclError:
                del kw['type']
        return'selection', 'get') + self._options(kw))
It does not look like this is meant to be used in programs.

From the documentation for tk: Wrote:selection get ?-displayof window? ?-selection selection? ?-type type?
Retrieves the value of selection from window's display and returns it as a result. Selection defaults to PRIMARY and window defaults to “.”. Type specifies the form in which the selection is to be returned (the desired “target” for conversion, in ICCCM terminology), and should be an atom name such as STRING or FILE_NAME; see the Inter-Client Communication Conventions Manual for complete details. Type defaults to STRING. The selection owner may choose to return the selection in any of several different representation formats, such as STRING, UTF8_STRING, ATOM, INTEGER, etc. (this format is different than the selection type; see the ICCCM for all the confusing details). If the selection is returned in a non-string format, such as INTEGER or ATOM, the selection command converts it to string format as a collection of fields separated by spaces: atoms are converted to their textual names, and anything else is converted to hexadecimal integers. Note that selection get does not retrieve the selection in the UTF8_STRING format unless told to.
Thanks for reply.

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 1,000 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  [Tkinter] Different rows colours in treeview tkinter Krissstian 1 459 Nov-20-2022, 09:59 PM
Last Post: woooee
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 2,212 Oct-20-2021, 12:14 AM
Last Post: CyKlop
  tkinter - update/refresh treeview snakes 4 14,130 May-13-2021, 07:10 AM
Last Post: snakes
  [Tkinter] acceleration of data output in treeview tkinter Vladimir1984 4 2,980 Nov-21-2020, 03:43 PM
Last Post: Vladimir1984
  [Tkinter] Tkinter timetabl using treeview mntfr 3 2,576 Feb-05-2019, 09:36 PM
Last Post: Larz60+
  sQlite3 output to tkinter treeview - how do I set / increase width of the output? dewijones67 5 5,616 Jan-23-2019, 08:45 AM
Last Post: Larz60+
  bind hover on tkinter.ttk.Treeview Larz60+ 4 13,151 May-20-2017, 10:28 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020