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

后端 未结 23 3293
温柔的废话
温柔的废话 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

    Actually, pywin32 and ctypes seem to be an overkill for this simple task. Tkinter is a cross-platform GUI framework, which ships with Python by default and has clipboard accessing methods along with other cool stuff.

    If all you need is to put some text to system clipboard, this will do it:

    from Tkinter import Tk
    r = Tk()
    r.withdraw()
    r.clipboard_clear()
    r.clipboard_append('i can has clipboardz?')
    r.update() # now it stays on the clipboard after the window is closed
    r.destroy()
    

    And that's all, no need to mess around with platform-specific third-party libraries.

    If you are using Python 3, replace TKinter with tkinter.

提交回复
热议问题