platform-independent image copy/paste with Tkinter?

eb303 eric.brunel.pragmadev at gmail.com
Mon May 17 06:18:37 EDT 2010


On May 15, 4:56 pm, technocake <robin.ga... at gmail.com> wrote:
> Hi.
>
> Is there a good way to copy from or paste to the clipboard on all
> systems running python using Tkinter?
> started a small road too it here:
>
> #!/usr/bin/python
> __author__="technocake"
> __date__ ="$15.mai.2010 15:53:39$"
>
> from Tkinter import *
>
> if __name__ == "__main__":
>
>     def displayClipboard(event):
>         try:
>             data = event.widget.clipboard_get(type="STRING")
>         except :
>             #Need a method for displaying a image here..
>             data = "Image" #
>         finally: #Updates the textlabel
>             lbl.config(text=data)
>
>     root = Tk()
>     lbl= Label(root, width=20)
>     lbl.bind("<Button-1>", displayClipboard) #binding to leftclick
>     lbl.pack()
>
>     root.mainloop()

The copy part is done via the methods clipboard_clear and
clipboard_append, available on all Tkinter widgets. By the way, the
default value for the 'type' option is 'STRING', so you actually don't
need it here, and neither do you in clipboard_append.

To be able to copy/paste images is much more platform dependent. There
are actually types for clipboard contents that handle images, but they
will vary from platform to platform and sometimes from application to
application. You can even have several types for the same contents. To
get these, you can use:
widget.selection_get(selection='CLIPBOARD', type='TARGETS')
You can try to copy an image on various platforms with various
applications, then print the value for this expression to figure out
what you can do with it.

HTH
 - Eric -



More information about the Python-list mailing list