Using PythonWin32 to copy text to Windoze Clipboard for Unix-stylerandom .sig rotator?

Fredrik Lundh fredrik at pythonware.com
Thu Apr 6 06:19:28 EDT 2006


dananrg at yahoo.com wrote:

> Can you tell I miss Unix?

by your early-nineties spelling of Windows ?

> I want to write a Python script that, when launched, will choose a
> random .sig (from a list of about 30 cool ones I've devised), and store
> the .sig text in the Windows Clipboard, so I can then paste it into any
> Windows application.

since most Python distributions comes with Tkinter, you can use Tkinter's
clipboard interface.  unfortunately (at least for this use case), Tkinter re-
moves things it has added to the clipboard when the program terminates,
so you have to make sure that the program is still running when you need
the text.

here's a fortune generator that keeps posting important stuff to the clip-
board at random intervals:

    import Tkinter
    import random, time

    # get some phrases
    import StringIO, sys
    stdout = sys.stdout
    try:
        sys.stdout = StringIO.StringIO()
        import this
        FORTUNES = sys.stdout.getvalue().split("\n")[2:]
    finally:
        sys.stdout = stdout

    # create an invisible (well, not really) window
    root = Tkinter.Tk()
    root.withdraw()

    def refresh():
        fortune = random.choice(FORTUNES)
        root.clipboard_clear()
        root.clipboard_append(fortune)
        root.after(random.randint(100, 1000), refresh)

    refresh()

    root.mainloop()

(since this will make it impossible to use the clipboard for anything else, you
might wish to use a button instead of a timer to update the clipboard...)

</F>






More information about the Python-list mailing list