[Tkinter-discuss] clipboard and bytes

Vasilis Vlachoudis Vasilis.Vlachoudis at cern.ch
Wed Feb 28 05:59:31 EST 2018


Hi all,

I took the decision to convert my program to python3. I am stuck with the clipboard handling str vs bytes
How can I get/receive bytes instead of str with the clipboard?

What I was doing up to now was opening a StringIO stream dump a header and the objects as a dump from Pickler

Up to now in python V2. it was working like this
Copy to clilpboard:
target1 = "<flair:card>"
sio = io.StringIO()
sio.write(target1)
pickler = Pickler(sio)
pickler.dump(....)
root.clipboard._clear()
root.clipboard_append(sio.getvalue())

Paste from clipboard:
clip = root.clipboard_get(selection='CLIPBOARD')
if clip.startswith(target1):
   pickler = Unpickler(io.StringIO(clipboard[len(target1):]))
   ...
elif...

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.

Thanks in advance
Vasilis



More information about the Tkinter-discuss mailing list