How do I copy a string to the clipboard on Windows using Python?

后端 未结 23 2802
温柔的废话
温柔的废话 2020-11-22 03:13

I\'m trying to make a basic Windows application that builds a string out of user input and then adds it to the clipboard. How do I copy a string to the clipboard using Pytho

23条回答
  •  一向
    一向 (楼主)
    2020-11-22 03:36

    This is the improved answer of atomizer.

    Note 2 calls of update() and 200 ms delay between them. They protect freezing applications due to an unstable state of the clipboard:

    from Tkinter import Tk
    import time     
    
    r = Tk()
    r.withdraw()
    r.clipboard_clear()
    r.clipboard_append('some string')
    
    r.update()
    time.sleep(.2)
    r.update()
    
    r.destroy()
    

提交回复
热议问题