Search code examples
pythonbindingtkintercursor

Change Cursor when Hovering Over Part of Frame TKinter


How can you make the cursor change when you hover over a certain part of a frame? I am trying to make it so there is a background image and when someone hovers over a certain part that, if clicked on, will make the frame change, the cursor will change to signal that interaction will occur if they click.

class Map(Frame):
    def __init__(self, master, controller, *args, **kwargs):
        Frame.__init__(self, master)
        self.controller = controller
        #keep player data stored at all times
        self.player = Player.Player()
        # define map gui here
        #repack this frame so binding included
        self.map_picture = PhotoImage(file=r"images/rsz_archipelago.gif")
        self.image = Label(self, image=self.map_picture)
        self.image.bind("<Button-1>", self.check_for_spot)
        self.image.grid(row=0, column=0, columnspan=40, rowspan=40)

        self.places = {MapRectange(420,490,175, 205):Hydra_Level}


    def check_for_spot(self, event):
        x1, y1 = event.x, event.y
        pt = Point(x1,y1)
        print("X: " + str(x1) + "  Y: " + str(y1))
        for place, level in self.places.iteritems():
            if place.point_is_in(pt):
                self.controller.show(level, self)
                break

Solution

  • To get your cursor coordinates:

    import Tkinter as tk
    from Tkinter import Frame
    root = tk.Tk()
    frame1 = Frame(root, bg='blue',width='100px', height='100px')
    frame1.pack()
    
    def motion(event):
        x, y = event.x, event.y
        print('{}, {}'.format(x, y))
    
    
    frame1.bind('<Motion>', motion)
    root.mainloop()
    

    you will see in your console, the coordinates while you move the cursor, afterwards, you can define ranges and have the cursor change based or the image... please refer to http://effbot.org/tkinterbook/ there is excellent documentation on pythons Tkinter.