ttkbootstrap icon indicating copy to clipboard operation
ttkbootstrap copied to clipboard

Add cross-platform clipboard support on Tableview

Open israel-dryer opened this issue 2 years ago • 6 comments

Add option to copy selection to clipboard.

A code example (maybe there are other better ways), I don't know what it's like on Windows and Mac. But on Linux you need to use pyperclip to keep the data on the clipboard after closing the program. And on linux you also need to do this 'update()' twice and need the return 'break'

example:

import pyperclip

def tvw_item_ckick(self, event=None):
    self.tvw.selection()
    str_all_columns = ''
    for n in self.tvw.selection():
        str_columns = '\t'.join(self.tvw.item(n, 'values'))
        str_all_columns += f'''{str_columns}\n'''

    self.clipboard_clear()
    self.update()

    try:
        pyperclip.copy(str_all_columns)
    except TclError as e:
        raise e
    
    self.update()
    return 'break'

self.tvw.bind('<Control-c>', self.tvw_item_ckick)
self.tvw.bind('<Control-C>', self.tvw_item_ckick)

Originally posted by @antrrax in https://github.com/israel-dryer/ttkbootstrap/issues/114#issuecomment-1004099982

israel-dryer avatar Jan 03 '22 13:01 israel-dryer

Adding for reference: https://bugs.python.org/issue40452

israel-dryer avatar Jan 03 '22 14:01 israel-dryer

Besides the option to copy the selection to the clipboard. An option to copy the selection with the header would be interesting, it would be the same code above, but the variable 'str_all_columns' would start with the value of the headers. str_all_columns = f'''{[col._headertext for col in self._tablecols]}\n'''

antrrax avatar Jan 07 '22 20:01 antrrax

@antrrax this should be expanded to all widgets if possible. Preferably without adding dependencies, but if not possible, then we can look at using the pyperclip solution referenced. It looks like the root of this may be the tkinter bug referenced above.

@pysimplegui, were you able to find a solution for this for PSG?

israel-dryer avatar Jan 26 '22 12:01 israel-dryer

https://wiki.archlinux.org/title/Clipboard

Interesting read. The behavior on Linux actually makes sense with respect to this protocol as the data is not actual copied until pasted, which is why it probably gets lost in tkinter.

israel-dryer avatar Jan 26 '22 13:01 israel-dryer

@antrrax, pyperclip relies on xclip and xsel which would have to be installed on Linux in many cases unless you are using a full featured distro such as Ubuntu or Linux Cinnamon, etc... I'm assuming that pyperclip handles these missing packages in some way? Perhaps an error message? I'll put this on the development list as something to review.

israel-dryer avatar Jan 26 '22 14:01 israel-dryer

On Ubuntu/mint need to install xclip and xsel. But as it is something that is used frequently, it is already in my system post installation script, so I forget that it doesn't come with the vanilla system.

antrrax avatar Jan 26 '22 15:01 antrrax