执行鼠标事件时更改光标
Change cursor when doing a mouse event
如何使用 Tkinter 在 Python 中更改鼠标事件(例如右键单击)的光标?当我按下右键时,光标会改变,但当我释放它时,光标不会改变。
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.new_width=800
self.new_height=600
# Main window
self.fen = Tk()
self.fen.title("Image")
canevas = Canvas(self.fen, bd=0) ## creation of the canvas
canevas.config(bg="white")
canevas.config(cursor="dotbox")
canevas.pack(fill=BOTH,expand=True) ## I place the canvas with the .pack() method
canevas.config(scrollregion=canevas.bbox("all"))
w=canevas.winfo_width()
h=canevas.winfo_height()
self.fen.update()
# This is what enables using the mouse:
canevas.bind("<ButtonPress-3>", self.move_start)
canevas.bind("<B3-Motion>", self.move_move)
# start :
def run(self):
self.fen.mainloop()
#move
def move_start(self,event):
self.canevas.config(cursor="fleur")
def move_move(self,event):
self.canevas.config(cursor="fleur")
使用
将一个事件绑定到按钮的释放,该按钮将光标变回"dotbox"
canevas.bind("<ButtonRelease-3>", self.move_stop)
那么你不需要 <B3-Motion>
事件,光标会一直停留 "fleur"
直到你释放按钮
在您发布的代码中,您需要将每次提到的 canevas
替换为 self.canevas
以便能够在 move_start
函数(以及任何其他 class 方法)
如何使用 Tkinter 在 Python 中更改鼠标事件(例如右键单击)的光标?当我按下右键时,光标会改变,但当我释放它时,光标不会改变。
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.new_width=800
self.new_height=600
# Main window
self.fen = Tk()
self.fen.title("Image")
canevas = Canvas(self.fen, bd=0) ## creation of the canvas
canevas.config(bg="white")
canevas.config(cursor="dotbox")
canevas.pack(fill=BOTH,expand=True) ## I place the canvas with the .pack() method
canevas.config(scrollregion=canevas.bbox("all"))
w=canevas.winfo_width()
h=canevas.winfo_height()
self.fen.update()
# This is what enables using the mouse:
canevas.bind("<ButtonPress-3>", self.move_start)
canevas.bind("<B3-Motion>", self.move_move)
# start :
def run(self):
self.fen.mainloop()
#move
def move_start(self,event):
self.canevas.config(cursor="fleur")
def move_move(self,event):
self.canevas.config(cursor="fleur")
使用
将一个事件绑定到按钮的释放,该按钮将光标变回"dotbox"
canevas.bind("<ButtonRelease-3>", self.move_stop)
那么你不需要 <B3-Motion>
事件,光标会一直停留 "fleur"
直到你释放按钮
在您发布的代码中,您需要将每次提到的 canevas
替换为 self.canevas
以便能够在 move_start
函数(以及任何其他 class 方法)