Clipboard Monitor (tkinter,windows)

Peter Sutton annannienann at yahoo.co.uk
Fri Aug 24 11:35:05 CEST 2007


Hi. I am an A-level student studying computer science. I have taken it upon myself to learn python. It is now my weapon of choice. I have had a problem trying to creating a clipboard monitor in Windows(XP,Vista) that is linked to a tkinter GUI. I have a perfectly good code for clipboard monitoring, but as soon as it is linked to a tkinter gui, the gui locks when a WM.DRAWCLIPBOARD message is recieved. I am at the point of giving up. If anyone can help or offer an alternative....that is not polling....I would be very happy.  Thanks. Peter

import win32ui, win32clipboard, win32con, win32api, win32gui
from Tkinter import *

def paste():
    win32clipboard.OpenClipboard(0)
    data = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()
    return data


class ClipRecord(object):
    def __init__(self):
        self.gui = Tk()
        self.hPrev = 0
        self.first = True
        self.win = win32ui.CreateFrame()
        self.win.CreateWindow(None,'',win32con.WS_OVERLAPPEDWINDOW)
        self.win.HookMessage(self.OnDrawClipboard,win32con.WM_DRAWCLIPBOARD)
        self.win.HookMessage(self.OnChangeCBChain,win32con.WM_CHANGECBCHAIN)
        self.win.HookMessage(self.OnDestroy,win32con.WM_DESTROY)
        try:
            self.hPrev=win32clipboard.SetClipboardViewer(self.win.GetSafeHwnd())
        except win32api.error, err:
            if win32api.GetLastError () == 0:
                # information that there is no other window in chain
                pass
            else:
                raise
        self.l = Label(self.gui,text="I have losted the will to live")
        self.l.pack()
        self.gui.mainloop()
    def OnChangeCBChain(self, *args):
        msg, wParam, lParam = args[-1][1:4]
        if self.hPrev == wParam:
            # repair the chain
            self.hPrev = lParam
        if self.hPrev:
            # pass the message to the next window in chain
            win32api.SendMessage (self.hPrev, msg, wParam, lParam)

    def OnDrawClipboard(self, *args):
        msg, wParam, lParam = args[-1][1:4]
        if self.first:
            self.first = False
        else:
            self.l["text"] = "Lord be Praised"
            print paste()
        if self.hPrev:
            # pass the message to the next window in chain
            win32api.SendMessage (self.hPrev, msg, wParam, lParam)
    def OnDestroy(self):
        if self.hPrev:
            win32clipboard.ChangeClipboardChain(self.win.GetSafeHwnd(),self.hPrev)
        else:
            win32clipboard.ChangeClipboardChain(self.win.GetSafeHwnd(),0)

if __name__ == "__main__":
    cr = ClipRecord()




      ___________________________________________________________
Yahoo! Answers - Got a question? Someone out there knows the answer. Try it
now.
http://uk.answers.yahoo.com/ 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070824/4c145ad0/attachment.html>


More information about the Python-list mailing list