Tk Only Copies To Clipboard If "paste" Is Used Before Program Exits
Solution 1:
Your issue about the clipboard being empty if you close the tk app before pasting the clipboard is due to an issue in tkinter itself. This has been reported a few times and it has to due with the lazy way tkinter handles the clipboard.
If something is set to the tkinter clipboard but is not pasted then tkinter will not append the windows clipboard before the app is closed. So one way around that is to tell tkinter to append to the windows clipboard.
I have been testing a method to do that however it is causing some delay in the applications process so its probably not the best solution but is a start. Take a look at this modified version of your code using the import os
with the system
method.
from tkinter import *
from tkinter import messagebox
import os
top = Tk()
top.geometry("400x75")
top.title = "Get Test URL"
url = 'http://testServer/feature/'
fullURL = StringVar()
fullURL.set(url)
def copyToClipboard():
top.clipboard_clear()
top.clipboard_append(fullURL.get())
os.system('echo {}| clip'.format(fullURL.get()))
top.update()
top.destroy()
def updateURL(event):
fullURL.set(url + featureNumber.get())
def submit(event):
copyToClipboard()
topRow = Frame(top)
topRow.pack(side = TOP)
bottomRow = Frame(top)
bottomRow.pack(side = BOTTOM)
featureLabel = Label(topRow, text="Feature Number")
featureLabel.pack(side = LEFT)
featureNumber = Entry(topRow)
featureNumber.pack(side = RIGHT)
fullLine = Label(bottomRow, textvariable=fullURL)
fullLine.pack(side = TOP)
copyButton = Button(bottomRow, text = "Copy", command = copyToClipboard)
copyButton.pack(side = TOP)
featureNumber.focus_set()
featureNumber.bind("<Return>", submit)
top.mainloop()
When you run the code you will see that the code freezes the app but once its finishes processing after a few seconds it will have closed the app and you can still paste the clipboards content. this servers to demonstrate that if we can write to the windows clipboard before the tkinter app is closed it will work as intended. I will look for a better method but this should be a starting point for you.
Here is a few links of the same issue that has been reported to tkinter.
UPDATE:
Here is a clean solution that uses the library pyperclip
This is also cross platform :)
from tkinter import *
from tkinter import messagebox
import pyperclip
top = Tk()
top.geometry("400x75")
top.title = "Get Test URL"
url = 'http://testServer/feature/'
fullURL = StringVar()
fullURL.set(url)
def copyToClipboard():
top.clipboard_clear()
pyperclip.copy(fullURL.get())
pyperclip.paste()
top.update()
top.destroy()
def updateURL(event):
fullURL.set(url + featureNumber.get())
def submit(event):
copyToClipboard()
topRow = Frame(top)
topRow.pack(side = TOP)
bottomRow = Frame(top)
bottomRow.pack(side = BOTTOM)
featureLabel = Label(topRow, text="Feature Number")
featureLabel.pack(side = LEFT)
featureNumber = Entry(topRow)
featureNumber.pack(side = RIGHT)
fullLine = Label(bottomRow, textvariable=fullURL)
fullLine.pack(side = TOP)
copyButton = Button(bottomRow, text = "Copy", command = copyToClipboard)
copyButton.pack(side = TOP)
featureNumber.focus_set()
featureNumber.bind("<Return>", submit)
top.mainloop()
Post a Comment for "Tk Only Copies To Clipboard If "paste" Is Used Before Program Exits"