How to get a unique id for bound methods?

Russell E. Owen rowen at
Mon Aug 22 19:30:19 CEST 2005

In article <43067d0e.1906448017 at>,
 bokr at (Bengt Richter) wrote:

>On Fri, 19 Aug 2005 16:33:22 -0700, "Russell E. Owen" <rowen at> 
>>The current issue is associated with Tkinter. I'm trying to create a tk 
>>callback function that calls a python "function" (any python callable 
>>To do that, I have to create a name for tk that is unique to my python 
>>"function". A hash-like name would be perfect, meaning a name that is 
>>always the same for a particular python "function" and always different 
>>for a different python "function". That would save a lot of housekeeping.
>Why do you need a name? Can you post an example snippet that shows
>a callback function being used with Tkinter as you would wish?
>I have a feeling there is a much simpler solution than you are imagining ;-)

Here is an example (simplified from my real code; I may have introduced 
an error in the process). I could switch to the twisted framework, but 
this code has been working very well and it saves my users from having 
to install a 3rd party package.

-- Russell

def addTkCallback(tk, func):
    tkName = "cb%d" % hash(func)
    tk.createcommand(tkNname, func)
    return tkName

class TkSocket(TkBaseSocket):
    def __init__(self,
        readCallback = None,
        stateCallback = None,
        tkSock = None,
            # create the socket and configure it
            self._sock ="socket", ...)
  "fconfigure", self._sock, ...)
            # add callbacks; the write callback is just used to detect 
            readName =addTkCallback(self._tk, self._doRead)
  'fileevent', self._sock, "readable",
            connName = addTkCallback(self._tk, self._doConnect)
  'fileevent', self._sock, "writable", connName
        except Tkinter.TclError, e:
            raise RuntimeError(e)

More information about the Python-list mailing list