5

I'm trying to take a string from the Windows clipboard and paste it into a listbox in a Tkinter GUI. It works great until trying to copy a image.

clipboardData = root.selection_get(selection="CLIPBOARD")
listbox.insert(0, clipboardData)

I have tried to use Tkinter, pyperclip and clipboard. How can I avoid non-text content?

5
  • 1
    At the simplest, why not try: the listbox.insert and handle any error with except WhateverError:? See e.g. docs.python.org/2/tutorial/errors.html#handling-exceptions Commented Feb 3, 2015 at 13:54
  • try: clipboardData = clipboard.get() # root.selection_get(selection="CLIPBOARD") except: print "Can't copy images" still crashes... Commented Feb 3, 2015 at 14:00
  • 1
    Did you try putting the insert inside the try? Commented Feb 3, 2015 at 14:01
  • my mistake! the try-except solution works! there is another solution? Commented Feb 3, 2015 at 14:07
  • 1
    How many solutions do you need? Commented Feb 3, 2015 at 14:23

1 Answer 1

1

Using Tkinter, I would use a try..except block to insert the clipboard data where it exists and ignore it where it doesn't (or, optionally, add some default value). This doesn't specifically revoke any image-type clipboard contents, but it will reject anything that isn't in myTkObject.clipboard_get()'s expected format. That's a string by default, though you can change it with the function's type keyword argument.

Here's an example that builds on Nodak and jonrsharpe's answer and comments:

from Tkinter import Tk
myTkObject = Tk()
try:
    listbox.insert(0, myTkObject.clipboard_get())
except Tkinter.TclError:
    pass  # Instead of do-nothing, you can insert some default value if you like.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.