[Tkinter-discuss] clipboard and bytes

Michael Lange klappnase at web.de
Wed Feb 28 14:42:51 EST 2018


Hi,

On Wed, 28 Feb 2018 10:59:31 +0000
Vasilis Vlachoudis <Vasilis.Vlachoudis at cern.ch> wrote:

(...)
> In python3 Pickler requires a byte stream so I replaced all StringIO()
> to BytesIO() and the targets with bytes target1 = b"<flair:card>"
> Copying to clipboard work ok (or I believe so)
> Pasting, clipboard_get() returns a str not bytes and the Unpickler
> fails. Also If I try to encode("UTF-8") the clip data also it fails.

according to man clipboard it is possible create custom data types to be
stored in the tk clipboard:
  "You can put custom data into the clipboard by using a custom -type
  option. This is not necessarily portable, but can be very useful. The
  method of passing Tcl scripts this way is effective, but should be mixed
  with safe interpreters in production code."

I set up a (rather useless) example how this can be done:

##################################
from tkinter import *

root = Tk()

def copy(string):
    def cp():
        return 666
    copyfunc = (root.register(cp))
    return(copyfunc)

root.clipboard_clear()
root.clipboard_append(copy('foobar'), type='foo')

def paste(ev):
    clip = root.clipboard_get(type='foo')
    res = root.tk.call(clip)
    print('->', res, type(res))

root.bind('<F1>', paste)
root.mainloop()
#################################

When I run this script and hit F1 I get the following output:

  $ python3 test5.py
  -> 666 <class 'int'>

So at least this primitive seems to work. Maybe you can use this
technique to achieve what you want.

Regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Without followers, evil cannot spread.
		-- Spock, "And The Children Shall Lead", stardate 5029.5


More information about the Tkinter-discuss mailing list