1

I'm trying to make a link slicer that automatically slice and copy the sliced output of the link right after I pasted it on txt = Entry(window,width=50) without clicking a button.

When I paste a link something like this: http://url.io/s/1234abcd/?s=https%3A%2F%2Fexample.com%2Fsome_contens%2F then it will become: example.com/some_contents/

Right now the code below is what I use, but I still have to click the "slice" button before "slicing" and "copying" happens.

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

window = Tk()

window.title("Link Slicer")

window.geometry('344x50')

lbl = Label(window, text="Link")

lbl.grid(column=0, row=1)

txt = Entry(window,width=50)

txt.grid(column=0, row=0)

def clicked():

    sliced = txt.get()

    sliced = parse.unquote(sliced)

    lbl.configure(text= sliced[36:])

    r = Tk()
    r.withdraw()
    r.clipboard_clear()
    r.clipboard_append(sliced[36:])
    r.update()

btn = Button(window, text="Slice", command=clicked)

btn.grid(column=1, row=0)



window.mainloop()

I made this code below to try to automate "slicing" and "copying" right after I paste the link in the txt = Entry(window,width=50) without clicking a button (which doesn't work):

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

window = Tk()

window.title("Link Slicer")

window.geometry('344x50')

lbl = Label(window, text="Link")

lbl.grid(column=0, row=1)

txt = Entry(window,width=50)

txt.grid(column=0, row=0)


sliced = txt.get()              // 1. automatically get what's in txt = Entry(window,width=50)

sliced = parse.unquote(sliced)  // 2. auto-slice


r = Tk()                           }
r.withdraw()                       }
r.clipboard_clear()                } // 3. auto-copy
r.clipboard_append(sliced[36:]).   }
r.update()                         }


window.mainloop()
which_part
  • 792
  • 3
  • 11
  • 26
Pentium
  • 71
  • 2
  • 5
  • BTW, you should not call `Tk()` more than once. That call doesn't just create the root window, it also creates an instance of the Tcl interpreter, as Bryan explains [here](https://stackoverflow.com/a/48045508/4014959). – PM 2Ring Jul 30 '18 at 04:48
  • Do you want the `clicked` operation to happen automatically whenever _any_ change is made to the Entry contents? Will you only ever be pasting a complete URL into that Entry, or do you also want to be able to type in URLs? – PM 2Ring Jul 30 '18 at 04:50
  • @PM2Ring no, i won't type there, just paste. – Pentium Jul 30 '18 at 04:55

1 Answers1

0

Since you always want to operate on the clipboard contents there's no need to even paste the clip into the Entry. You can just grab the clip, transform it, and put it back into the clipboard. I've included an Entry to display the result, but that's not really necessary.

import tkinter as tk
from urllib import parse

root = tk.Tk()
root.title("Link Slicer")
root.geometry('344x50')
root.attributes("-topmost", True)

def clicked():
    clip = root.clipboard_get()
    clip = parse.unquote(clip)[36:]

    root.clipboard_clear()
    root.clipboard_append(clip)
    entry_text.set(clip)

entry_text = tk.StringVar()
txt = tk.Entry(root, textvariable=entry_text, width=50)
txt.pack()

btn = tk.Button(root, text="Slice", command=clicked)
btn.pack()

root.mainloop()

When I copy the sample URL from your question and click the Slice button I get this result displayed in the Entry and copied to the clipboard. Note that I did not paste the URL into the Entry, I just clicked the button.

example.com/some_contens/

I've included root.attributes("-topmost", True) to force the window to stay above other windows, because I think that's convenient for a tool like this.

As I mentioned in the comments, you should not call Tk() more than once because each call starts a new instance of the Tcl interpreter, which can lead to confusing behaviour (and it's a waste of resources).

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182