mdabdullah
mdabdullah

Reputation: 626

Tkinter how to popout a Toplevel window by cursor movement instead of button click

I have the following code in which when I click the button, the function is called and a window pops up. What I want is, when I click tab and move from the first entry box to the second entry box, I need this function to be called. My intention is to eliminate the need for the "Click" button.

from Tkinter import *

def fn_jump():
    window3 = Toplevel()
    window3.title("This window popped out")
    secondentry = Entry(window3)
    secondentry.grid(row=0)
    secondentry.focus_set()

root = Tk()
txtbox1 = Entry(root)
txtbox2 = Entry(root)

txtbox1.grid(row=0)
txtbox2.grid(row=1)
txtbox2.focus_force=fn_jump   #This does not work

btn1 = Button(root, text = 'Click' , command=fn_jump)
btn1.grid(row=2)

root.mainloop()

Upvotes: 0

Views: 216

Answers (1)

Novel
Novel

Reputation: 13729

You can do that by using bind to run a function when the Entry gets focus.

txtbox2.bind('<FocusIn>', fn_jump)

To use bind, the function you are calling must accept an event argument, so change that definition to

def fn_jump(event=None):

Upvotes: 1

Related Questions