Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
691 views
in Technique[技术] by (71.8m points)

tkinter - Accessing text copied to clipboard by python

So I want to be able to use a python script to copy the contents of a folder and then be able to paste those contents to a location of my choosing i.e. text file, browser, etc... I came across this solution for copying text to the clipboard, but when i implement this solution I am not able to paste anything. I am using python 3.4. Below is code i am using:

import os 
import tkinter as tk
import tkinter.filedialog

r = tk.Tk()
r.withdraw()
photo_path= tkinter.filedialog.askdirectory(title='Which folder would you like to copy the contents from?', initialdir='/')

# Get list of filenames in current directory
file_list=[]

for filename in os.listdir(photo_path):
    if os.path.splitext(filename)[1]=='.JPG':
        file_list.append(os.path.splitext(filename)[0])
    else: pass

file_search='code:('+' OR '.join(file_list)+')'

r.clipboard_clear()
r.clipboard_append(file_search)
r.destroy()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you don't use the clipboard content before your script ends, it is discarded. Keep it running until you no longer need the clipboard content. The following program will keep '1234' in the clipboard for 10 seconds. If you don't paste it within that time, it is lost. If you do paste it within that time, it will remain in the clipboard even after the program ends.

import tkinter as tk

r = tk.Tk()
r.withdraw()

r.clipboard_clear()
r.clipboard_append('1234')
r.after(10000, lambda: r.destroy())
r.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...