Cursor event handling in python+Tkinter

I'm building a code in which I'd like to be able to generate an event when the user changes the focus of the cursor from an Entry widget to anywhere, for example another entry widget, a button...

So far i only came out with the idea to bind to TAB and mouse click, although if i bind the mouse click to the Entry widget i only get mouse events when inside the Entry widget.

How can I accomplish generate events for when a widget loses cursor focus?

Thanks in advance!


Asked by: Wilson163 | Posted: 28-01-2022






Answer 1

The events <FocusIn> and <FocusOut> are what you want. Run the following example and you'll see you get focus in and out bindings whether you click or press tab (or shift-tab) when focus is in one of the entry widgets.

from Tkinter import *

def main():
    global text

    root=Tk()

    l1=Label(root,text="Field 1:")
    l2=Label(root,text="Field 2:")
    t1=Text(root,height=4,width=40)
    e1=Entry(root)
    e2=Entry(root)
    l1.grid(row=0,column=0,sticky="e")
    e1.grid(row=0,column=1,sticky="ew")
    l2.grid(row=1,column=0,sticky="e")
    e2.grid(row=1,column=1,sticky="ew")
    t1.grid(row=2,column=0,columnspan=2,sticky="nw")

    root.grid_columnconfigure(1,weight=1)
    root.grid_rowconfigure(2,weight=1)

    root.bind_class("Entry","<FocusOut>",focusOutHandler)
    root.bind_class("Entry","<FocusIn>",focusInHandler)

    text = t1
    root.mainloop()

def focusInHandler(event):
    text.insert("end","FocusIn %s\n" % event.widget)
    text.see("end")

def focusOutHandler(event):
    text.insert("end","FocusOut %s\n" % event.widget)
    text.see("end")


if __name__ == "__main__":
    main();

Answered by: Maria138 | Posted: 01-03-2022



Answer 2

This isn't specific to tkinter, and it's not focus based, but I got an answer to a similar question here:

Detecting Mouse clicks in windows using python

I haven't done any tkinter in quite a while, but there seems to be "FocusIn" and "FocusOut" events. You might be able to bind and track these to solve your issue.

From: http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

Answered by: Ada111 | Posted: 01-03-2022



Similar questions

Better way to deal with Tk's keyboard events mess for numpad keys in Python+tkinter?

I am making a tkinter application that responds to individual key presses on the numeric keypad. I find that handling the tk events for the numpad keypresses are a real pain because all platforms that I tried it on result in totally different events (windows, mac, linux), and the documentation I could find is either incomplete or just wrong. My main doc source is


How to draw colored Rectangular on Transparent canvas with Python+tkinter

I want to draw Colored Rectangular on Transparent Canvas to make Capture Tool with python and tkinter. But when I draw Colored Rectangular on Transparent Canvas Colored Rectangular are not Shown , because Canvas is Transparent. I need Transparent Canvas with Non-Transparent Rectangular on it. thanks. My steps... Draw Transparent Canvas (canvas size is full screen) and ...


function - Value from new window, button, and script to main script/window python+tkinter

I have a main script. When you push the button (in tkinter) you open a class with a new window and a new button. When you click the new button (in the new window and different file) the text in the main window should be updated. I have the following: Main script from tkinter import * from kandit import Kandit root=Tk() def hoop(): s=Kandit() label.configure(text=s) button=Button(root...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top