Need Function To Run As Long As Button Widget Pressed

Josiah josiah7 at hotmail.com
Wed Nov 20 20:04:30 EST 2002


I would like for a function to run as long as the button widget is
pressed.
My current approach is to call the function with the button widget,
then the function checks the status of variable to determine if it
should loop. The variable is set by an event binding. Unfortunatly, my
code doesn't work. I am getting this error:

----------------------------------------------------------------------------
Exception in Tkinter callback
Traceback (innermost last):
  File "Runtime\Python\lib\lib-tk\Tkinter.py", line 764, in __call__
    return apply(self.func, args)
TypeError: too many arguments; expected 1, got 2
-----------------------------------------------------------------------------

Heres my code. This should print "working" to the console as long as
the button widget is pressed;

###################################################
from Tkinter import *
root = Tk()

class App:

  def __init__(self, master):  

      frame = Frame(master) 
      frame.grid()

      self.var = IntVar()

      self.buttonGo = Button(frame, text= "Print", command=self.Go)
      self.buttonGo.grid()
   
      self.buttonGo.bind("<Button-1>", self.IsPressed)
      self.buttonGo.bind("<ButtonRelease-1>", self.IsReleased)
      self.buttonGo.bind("<Leave>", self.IsReleased) 

  def Go(self):
      print "Working"
      status = self.var.get()
      if status == 1: 
         self.after(10, self.Go())
   
  def IsPressed(event):
      self.var.set(1)
  
  def IsReleased(event):
      self.var.set(0)
 
app = App(root)
root.mainloop()
root.quit
###################################################

 Doe's anyone see what the problem is? Maybe there is a better way of
doing what I want to do? I couldn't find any examples anywhere.

 Thanks for any help!
josiah



More information about the Python-list mailing list