Binding multiple events on Tkinter Entry? Binding multiple events on Tkinter Entry? tkinter tkinter

Binding multiple events on Tkinter Entry?


Firstly, you need to bind callback2 to the Enter / Return Key this is done using '<Return>'.

def __init__(self, master):    self.pass1 = tk.Entry(master,show="*")    self.pass1.bind('<Key>', self.callback1)    self.pass1.bind('<Return>', self.callback2) # callback2 bound to Enter / Return key    self.pass1.pack()

Next, you want to only allow callback1 to run once. To do this, unbind it from the widget like so

def callback1(self, event):    self.start=time.time()*1000.0     self.pass1.unbind('<Key>') # unbind callback1

And then finally rebind it once the Enter key is pressed, so in the callback2 function

def callback2(self,event): # called by Enter Key press    self.end=time.time()*1000.0    self.total_time=self.end-self.start    print(self.total_time)    self.pass1.bind('<Key>', self.callback1) # rebind callback1

Side Notes:

As you can see I changed the ordering for the time to end - start instead of what you had before which was start - end which gives you a negative value.

I also suggest changing '<Key>' to '<KeyRelease>'

Your other options if you don't want to unbind the function is to use an if statement checking if self.start has a value.

if self.start == 0.0:    self.start=time.time()*1000.0

You should also put your variables inside the __init__ function.

def __init__(self, master):    self.start=0.0    self.end=0.0    self.total_time=0.0    ...