Re: [Tkinter-discuss] clipboard and bytes
On Wed, 28 Feb 2018 20:42:51 +0100 Michael Lange wrote: (...) > So at least this primitive seems to work. Maybe you can use this > technique to achieve what you want. Or maybe this slightly modified example comes closer to what you are looking for: from tkinter import * root = Tk() def copy(string): def cp(): return string copyfunc = (root.register(cp)) return(copyfunc) root.clipboard_clear() root.clipboard_append(copy(b'foobar'), type='foo') def paste(ev): clip = root.clipboard_get(type='foo') res = root.tk.call(clip) print('->', res, type(res)) root.bind('', paste) root.mainloop() Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. "... freedom ... is a worship word..." "It is our worship word too." -- Cloud William and Kirk, "The Omega Glory", stardate unknown ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] clipboard and bytes
Hi, On Wed, 28 Feb 2018 10:59:31 + Vasilis Vlachoudis wrote: (...) > In python3 Pickler requires a byte stream so I replaced all StringIO() > to BytesIO() and the targets with bytes target1 = b"" > 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('', paste) root.mainloop() # When I run this script and hit F1 I get the following output: $ python3 test5.py -> 666 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 ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
[Tkinter-discuss] clipboard and bytes
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 = "" 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"" 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 ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss