python - Tk only copies to clipboard if "paste" is used before program exits -
environment
- python 3.6.2
- windows 10
the problem
i use tk method clipboard_append() copy string clipboard.
when program run python interpreter data copied clipboard correctly.
when run using "c:\python36.exe myprogram.py" however, weird behavior.
- if paste data while program still running, works expected.
- if paste data while program running, close program, can continue paste data.
- if close program after copying before pasting, clipboard empty.
question
how make tk copy clipboard regardless of happens containing window?
my code
from tkinter import * tkinter import messagebox url = 'http://testserver/feature/' def copytoclipboard(): top.clipboard_clear() top.clipboard_append(fullurl.get()) top.update() top.destroy() def updateurl(event): fullurl.set(url + featurenumber.get()) def submit(event): copytoclipboard() top = tk() top.geometry("400x75") top.title = "get test url" 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) fullurl = stringvar() fullurl.set(url) 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("<keyrelease>", updateurl) featurenumber.bind("<return>", submit) top.mainloop() purpose of program
my company has test server use new features. every time create new feature need post url on test server. urls identical save feature number, created python program generate url me , copy clipboard.
i can working if comment out "top.destroy" , paste url before manually closing window, avoid this. in perfect world press shortcut, have window pop up, enter feature number, press enter close window , paste new url, without taking hands off keyboard.
your issue clipboard being empty if close tk app before pasting clipboard due issue in tkinter itself. has been reported few times , has due lazy way tkinter handles clipboard.
if set tkinter clipboard not pasted tkinter not append windows clipboard before app closed. 1 way around tell tkinter append windows clipboard.
i have been testing method causing delay in applications process not best solution start. take @ modified version of code using import os system method.
from tkinter import * 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 run code see code freezes app once finishes processing after few seconds have closed app , can still paste clipboards content. servers demonstrate if can write windows clipboard before tkinter app closed work intended. better method should starting point you.
here few links of same issue has been reported tkinter.
update:
here clean solution uses library pyperclip
this cross platform :)
from tkinter import * 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()
Comments
Post a Comment