2

I am writing a python script that will check the clipboard contents and prints them in the console. The below is the script that I am using.

import time
from tkinter import Tk

while True:
    r = Tk()
    result = r.selection_get(selection="CLIPBOARD")
    print(result)
    time.sleep(2)

When I run it without copying any text, I get the below error:

return self.tk.call(('selection', 'get') + self._options(kw))
_tkinter.TclError: CLIPBOARD selection doesn't exist or form "STRING" not defined

I understand that it appears as there are no contents in the clipboard. Once after copying any text, the code runs fine. In order to overcome the issue, I rewrote the code in the following manner:

import time
from tkinter import Tk

r = Tk()
x = 1
while x < 2:
    r.clipboard_clear()
    r.clipboard_append("Starter Text")
    x += 1

while True:
    r.clipboard_clear()
    result = r.selection_get(selection="CLIPBOARD")
    print(result)
    time.sleep(2)

I wrote this so that I can start the file by having a starter text in the clipboard. This will help in stopping the error. Even though it stopped the error from occuring, the code now prints only "Starter Text" in a repeated manner. Even after copying contents into the clipboard, they do not seem to be getting printed.

Can I get some suggestions on how to avoid the error and at the same time print the values whenever I copy something into the clipboard.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
mbvee
  • 383
  • 2
  • 4
  • 16
  • Your "Starter Text" trick doesn't actually fix the problem - what if the user copied an image or other non-text item to the clipboard while your program is running? You need to use `try`/`except` to catch and ignore the error when there's no text on the clipboard. I'm not sure why you aren't seeing changes to the clipboard - perhaps the Tkinter mainloop needs to be running for this to work. – jasonharper Apr 10 '17 at 15:01

2 Answers2

3

You can't avoid the error since this is designed behavior, but you can handle the error.

import tkinter as tk
...
try:
    selection = r.selection.get(selection="CLIPBOARD")
except tk.TclError:
    selection = None
...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Hi, I used the code `while True: r = Tk() r.clipboard_clear() try: result = r.selection_get(selection="CLIPBOARD") print(result) time.sleep(2) except Tk.tclError: selection = None` , but I am getting an error stating - `AttributeError: type object 'Tk' has no attribute 'tclError'` – mbvee Apr 10 '17 at 21:24
  • 1
    @mbvee: that error message seems pretty self-explanatory. An instance of `Tk` does not have the attribute `TclError`. That's not what my code shows. My code clearly shows using `TclError` from the tkinter module. Plus, there's absolutely no reason to call `Tk()` in an infinite loop. It only needs to be created once. – Bryan Oakley Apr 10 '17 at 21:26
  • Thanks for the response. I missed the import statement. I also had `import time from tkinter import Tk` in the beginning of the file. – mbvee Apr 10 '17 at 21:31
1

I came across with the below script and it helped me to get what I aimed for.

import time
from tkinter import Tk

while True:
    r = Tk()
    try:
        result = r.selection_get(selection="CLIPBOARD")
        print(result)
        time.sleep(1)
    except:
        selection = None

I went on with having the try except block with a generic except. @Bryan Oakley's suggestion helped a lot.

mbvee
  • 383
  • 2
  • 4
  • 16