Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Is it possible to call a function from a Tkinter widget.bind and include args???

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


sab at NOSPAM

Feb 11, 2002, 1:23 PM

Post #1 of 5 (623 views)
Permalink
Is it possible to call a function from a Tkinter widget.bind and include args???

Is it possible to call a function from a Tkinter widget.bind and include
args???
i.e. i want to do something like this:

def printButtonAndCoords(event, button):
print "you rolled over %s and coords are %d:%d" % button, event.x,
eventy

Button1.bind("<Enter>", printButtonAndCoords("button1"))
Button2.bind("<Enter>", printButtonAndCoords("button2"))

is this possible???

i have tried this: but it seems a bit crap!:

[snip]
...
self.newFileButton.bind("<Enter>", self.getMouseCoords)
self.newFileButton.bind("<Leave>", self.deleteToolTip)
...
def getMouseCoords(self, event):
self.xPos=event.x_root-5
self.yPos=event.y_root+5
self.showToolTip(str(event.widget), self.xPos, self.yPos)

def showToolTip(self, id, xPos, yPos):
if id==".8387596.7951612": #this looks very breakable!
self.toolTip=tkToolTip.toolTip(xPos, yPos, liftAbove=self.master,
text="Create a new file")

def deleteToolTip(self, event):
self.toolTip.destroyToolTip()
del self.toolTip
[/snip]

help... :'-(

G. Willoughby


martin at v

Feb 11, 2002, 2:58 PM

Post #2 of 5 (593 views)
Permalink
Is it possible to call a function from a Tkinter widget.bind and include args??? [In reply to]

"G. Willoughby" <sab [at] NOSPAM> writes:

> Is it possible to call a function from a Tkinter widget.bind and include
> args???
> i.e. i want to do something like this:
>
> def printButtonAndCoords(event, button):
> print "you rolled over %s and coords are %d:%d" % button, event.x,
> eventy
>
> Button1.bind("<Enter>", printButtonAndCoords("button1"))
> Button2.bind("<Enter>", printButtonAndCoords("button2"))
>
> is this possible???

The common idiom is

Button1.bind("<Enter>", lambda ev:printButtonAndCoords(ev,"button1"))
Button2.bind("<Enter>", lambda ev:printButtonAndCoords(ev,"button2"))

Notice that event.widget will give you the widget that was the source
of the event, so you may not need additional parameters at all.

Regards,
Martin


<fredrik at pythonware

Feb 11, 2002, 3:03 PM

Post #3 of 5 (590 views)
Permalink
Is it possible to call a function from a Tkinter widget.bind and include args??? [In reply to]

G. wrote:

> Button1.bind("<Enter>", printButtonAndCoords("button1"))

this calls the printButtonAndCoords function, and passes the
result to the bind function.

(well, to be precise, it gives a TypeError exception)

try this instead:

Button1.bind("<Enter>", lambda e: printButtonAndCoords(e, "button1"))
Button2.bind("<Enter>", lambda e: printButtonAndCoords(e, "button2"))

(the lambda statement creates a callable object taking one
argument, which it passes on to the printButtonAndCoords
function)

you might prefer writing this as

callback = lambda e: printButtonAndCoords(e, "button1")
Button1.bind("<Enter>", callback)
callback = lambda e: printButtonAndCoords(e, "button2")
Button2.bind("<Enter>", callback)

or even:

def callback(e):
printButtonAndCoords(e, "button1")
Button1.bind("<Enter>", callback)

def callback(e):
printButtonAndCoords(e, "button2")
Button2.bind("<Enter>", callback)

hope this helps!

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->


eric.brunel at pragmadev

Feb 12, 2002, 3:03 AM

Post #4 of 5 (589 views)
Permalink
Is it possible to call a function from a Tkinter widget.bind and include args??? [In reply to]

Hi,

G. Willoughby wrote:

> Is it possible to call a function from a Tkinter widget.bind and include
> args???
> i.e. i want to do something like this:
>
> def printButtonAndCoords(event, button):
> print "you rolled over %s and coords are %d:%d" % button, event.x,
> eventy
>
> Button1.bind("<Enter>", printButtonAndCoords("button1"))
> Button2.bind("<Enter>", printButtonAndCoords("button2"))
>
> is this possible???

Since you didn't mention your Python version, maybe the lambda stuff
provided by Martin and Fredrik won't work (with pre-2.0 versions, the
lambda may not have the printButtonAndCoords function in its namespace,
isn't it?).
So here is another solution I used quite a lot. with pre-2.0 Python It
involves the creation of a callback class:

class GenericCallback:
def __init__(self, callback, *firstArgs):
self.__callback = callback
self.__firstArgs = firstArgs
def __call__(self, *lastArgs):
apply(self.__callback, self.__firstArgs + lastArgs)

Then you make your binding like follows:

Button1.bind("<Enter>", GenericCallback(printButtonAndCoords, "button1"))

Your printButtonAndCoords function should then take the string as first
argument and the Tkinter event as second. The binding will "call" the
instance of GenericCallback, i.e. call its __call__ method, which will in
turn call printButtonAndCoords with the arguments provided in the
constructor, then in the actual call.

The GenericCallback class is also generic enough to have many other uses
(in fact everywhere you must provide a function).

HTH
- eric -


sab at NOSPAM

Feb 12, 2002, 3:22 PM

Post #5 of 5 (580 views)
Permalink
Is it possible to call a function from a Tkinter widget.bind and include args??? [In reply to]

Thankyou all, your comments are very helpful! :) Especially the
GenericCallback class!

G. Willoughby


"G. Willoughby" <sab [at] NOSPAM> wrote in message
news:a4995n$gbg$1 [at] news5
> Is it possible to call a function from a Tkinter widget.bind and include
> args???
> i.e. i want to do something like this:
>
> def printButtonAndCoords(event, button):
> print "you rolled over %s and coords are %d:%d" % button, event.x,
> eventy
>
> Button1.bind("<Enter>", printButtonAndCoords("button1"))
> Button2.bind("<Enter>", printButtonAndCoords("button2"))
>
> is this possible???
>
> i have tried this: but it seems a bit crap!:
>
> [snip]
> ...
> self.newFileButton.bind("<Enter>", self.getMouseCoords)
> self.newFileButton.bind("<Leave>", self.deleteToolTip)
> ...
> def getMouseCoords(self, event):
> self.xPos=event.x_root-5
> self.yPos=event.y_root+5
> self.showToolTip(str(event.widget), self.xPos, self.yPos)
>
> def showToolTip(self, id, xPos, yPos):
> if id==".8387596.7951612": #this looks very breakable!
> self.toolTip=tkToolTip.toolTip(xPos, yPos, liftAbove=self.master,
> text="Create a new file")
>
> def deleteToolTip(self, event):
> self.toolTip.destroyToolTip()
> del self.toolTip
> [/snip]
>
> help... :'-(
>
> G. Willoughby
>
>
>
>

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.