Posts: 26
Threads: 15
Joined: Oct 2020
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
Posts: 1,144
Threads: 114
Joined: Sep 2019
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
Posts: 6,779
Threads: 20
Joined: Feb 2020
Feb-11-2022, 07:58 PM
(This post was last modified: Feb-11-2022, 09:25 PM by Yoriz.
Edit Reason: Added code tags, Formatting
)
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.
Posts: 26
Threads: 15
Joined: Oct 2020
|