0

My goal is to bind an Event to the window. For example I want a function called when the mouse pointer enters the window. The code below does this but sadly the function is also called whenever the mouse pointer enters the Button. I tried B.unbind("<Enter>") but it does not work. Any help would be appreciated

import tkinter as tk
root = tk.Tk()

def function(event):
    print("Hello World")

B = tk.Button(root, text ="Label")

root.bind("<Enter>",function)
root.geometry("100x100")
B.pack()

root.mainloop()
A. P
  • 113
  • 4

1 Answers1

3

One way to make this work is to check for event.widget and see if it is the root window, which is a instance of Tk.

import tkinter as tk

root = tk.Tk()

def function(event):
    if isinstance(event.widget,tk.Tk): #check if event widget is Tk root window
        print("Hello World")

B = tk.Button(root, text ="Label")

root.bind("<Enter>",function)
root.geometry("100x100")
B.pack()

root.mainloop()
Henry Yik
  • 22,275
  • 4
  • 18
  • 40