How to get a unique id for bound methods?
Russell E. Owen
rowen at cesmail.net
Mon Aug 22 13:30:19 EDT 2005
In article <43067d0e.1906448017 at news.oz.net>,
bokr at oz.net (Bengt Richter) wrote:
>On Fri, 19 Aug 2005 16:33:22 -0700, "Russell E. Owen" <rowen at cesmail.net>
>wrote:
>[...]
>>
>>The current issue is associated with Tkinter. I'm trying to create a tk
>>callback function that calls a python "function" (any python callable
>>entity).
>>
>>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,
addr,
port,
binary=False,
readCallback = None,
stateCallback = None,
tkSock = None,
):
...
try:
# create the socket and configure it
self._sock = self._tk.call("socket", ...)
self._tk.call("fconfigure", self._sock, ...)
# add callbacks; the write callback is just used to detect
state
readName =addTkCallback(self._tk, self._doRead)
self._tk.call('fileevent', self._sock, "readable",
readName)
connName = addTkCallback(self._tk, self._doConnect)
self._tk.call('fileevent', self._sock, "writable", connName
except Tkinter.TclError, e:
raise RuntimeError(e)
More information about the Python-list
mailing list