将鼠标悬停在Frame TKinter的一部分上时更改光标

时间:2016-06-03 01:20:22

标签: python binding tkinter cursor

当您将鼠标悬停在某个帧的某个部分上时,如何更改光标?我试图让它成为一个背景图像,当有人在某个部分盘旋时,如果点击,将使帧发生变化,光标将变为发出信号,如果他们点击就会发生互动。

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

2 个答案:

答案 0 :(得分:2)

获取光标坐标:

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()

您将在控制台中看到移动光标时的坐标,之后,您可以定义范围并根据光标或图像进行更改... 请参考http://effbot.org/tkinterbook/有关于pythons Tkinter的优秀文档。

答案 1 :(得分:0)

使用光标 attrubute,如下所示:

YourFrame.cursor = pencil

请参阅:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/cursors.html http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/frame.html