1

When I run the code without copying anything, it will give an error. It works fine if I initially copy some string then run the code (see this clip).

I want the code to run just fine even if I initially didn't copy any string, then If I did copy some it will slice the string.

from tkinter import *
from tkinter import Tk
from urllib import parse

root = Tk()
root.geometry('304x70')
lbl=Label(root, text = "Nothing Here")
lbl.pack()

def check_clipboard(window):
    clip = root.clipboard_get()
    clip = parse.unquote(clip)[45:]

    root.clipboard_clear()
    root.clipboard_append(clip)
    lbl.configure(text= clip)


def run_listener(window, interval):
    check_clipboard(window)
    root.after(interval, run_listener, window, interval)

# Not sure what to put here:
#try:
#   ???
#except:
#   ??? 


run_listener(root, 5000)


root.mainloop()

I look to some posts [1, 2] that uses try.. except.. but I can't find them working for my specific issue.

yalpsid
  • 235
  • 1
  • 13
  • Just using my eyes I cannot find where will throw an error. `""[45:]` is also a valid expression which will return an empty string. So which line throws an error? – Sraw Oct 18 '18 at 03:59

1 Answers1

0

Modify check_clipboard() to only process the clip if there's actually anything copied:

def check_clipboard(window):
    clip = root.clipboard_get()
    if len(clip)>45:                     # check if there's enough data in the string for the next line to work properly
        clip = parse.unquote(clip)[45:]

        root.clipboard_clear()
        root.clipboard_append(clip)
        lbl.configure(text= clip)
Mark
  • 125
  • 6