# buttonHover.py from tkinter import * class BHDemo: def __init__(self, rootWin): #Create a button to put in the root window! #Attach the "doit" method to the buttons "command" option so that #the doit method will be called when the button is clicked. self.button = Button(rootWin, text="Click Me!", command= self.doit) #Bind the mouseMotion function to the "" event for the #button: self.button.bind("", self.mouseMotion) self.button.pack() self.ourRoot = rootWin #Save a reference to the rootWin #which will be used by the doit method! #This method gets called when mouse motion is detected over the button! def mouseMotion(self,event): x = event.x y = event.y coordStr = str( (x,y) ) self.button.config(text=coordStr) #This method gets called when the button is clicked! def doit(self): print("Button was clicked!") self.button.config(fg="red") #Change the button! l = Label(self.ourRoot, text="You clicked!") l.pack() #Create the main root window, instantiate the object, and run the main loop! rootWin = Tk() app = BHDemo( rootWin ) rootWin.mainloop()