Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Please...Binding mouse event to widget

Status
Not open for further replies.

josiah7

Programmer
Nov 13, 2002
4
US

Still new at Python and TK, Can someone please help me on this?
I have a button widget that calls a function. The function runs once when the button is depressed. I want the function to loop as long as the button is held down. But I need some kind of event handler for "mouse-1" to tell me if the mouse is still depressed over the button.

How can I write a function that will continue to loop as long as the button widget is pressed?
Thanks!
Josiah7
 
Here is one way to do it:

Code:
proc DoUpdate {} {
# This proc will be called every 100ms whilst the button
# is held down

   incr ::bob

   set ::id [after 100 DoUpdate]
}

set id 0
set bob 0
button .b -textvariable bob
pack .b
bind .b <ButtonPress-1> {DoUpdate}
bind .b <ButtonRelease-1> {after cancel $::id}

The binding for the button press starts the proc running.
The binding for the button release will cancel the after command so stopping the proc running.

Hope this gives you some ideas.
 
Thanks ovey! I understand how this might work, but I get some syntax errors with this example when I include it in a sample script as written. I couldn't fix it so it will work in python with TK. As I said, I have little experience with python. I am not familiar with the &quot;proc&quot; token but with &quot;def&quot; to define a function. Nor with the &quot;incr&quot; token which I understand to increment &quot;bob&quot;.

If you could possibly find the time, I would greatly appreciate if you could perhaps shouw me how I can get this to work. Maybe a small &quot;test.py&quot; script?

Thanks again for your time and help with this problem!
josiah7
 
The example I gave was for Tcl/Tk since this is a Tcl/Tk forum !! You might be better off asking in the Python Forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top