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.
#1
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')

tree1.selection_get()
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1007, in selection_get return self.tk.call(('selection', 'get') + self._options(kw)) _tkinter.TclError: PRIMARY selection doesn't exist or form "STRING" not defined
Reply
#2
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)
        self.tree.configure(yscroll=scrollbar.set)
        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()
    Window(root)
    root.mainloop()

main()
BashBedlam likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
It is inherited from tk.XView. From the code:
Lib/tkinter/__init__.py 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':
            try:
                kw['type'] = 'UTF8_STRING'
                return self.tk.call(('selection', 'get') + self._options(kw))
            except TclError:
                del kw['type']
        return self.tk.call(('selection', 'get') + self._options(kw))
It does not look like this is meant to be used in programs.

From the documentation for tk:
https://tcl.tk/man/tcl8.7/TkCmd/selection.html 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.
Reply
#4
Thanks for reply.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - update/refresh treeview snakes 5 20,563 Dec-02-2023, 07:05 PM
Last Post: aynous19
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 4,219 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  [Tkinter] Different rows colours in treeview tkinter Krissstian 1 1,192 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 3,305 Oct-20-2021, 12:14 AM
Last Post: CyKlop
  [Tkinter] acceleration of data output in treeview tkinter Vladimir1984 4 4,100 Nov-21-2020, 03:43 PM
Last Post: Vladimir1984
  [Tkinter] Tkinter timetabl using treeview mntfr 3 3,192 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 6,579 Jan-23-2019, 08:45 AM
Last Post: Larz60+
  bind hover on tkinter.ttk.Treeview Larz60+ 4 14,933 May-20-2017, 10:28 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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