问题描述
如何使用 Tkinter 在 Python 中更改鼠标事件(例如右键单击)的光标?当我按右键时,光标会改变,但当我松开时,光标不会改变.
How to change the cursor for a mouse event (right click for example) in Python with Tkinter ? When I press the right click, the cursor is changing but when I release it, the cursor doesn't change.
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")
推荐答案
绑定一个事件到按钮的释放,使用
Bind an event to the release of the button which changes the cursor back to "dotbox"
using
canevas.bind("<ButtonRelease-3>", self.move_stop)
那么你就不需要事件,光标会一直停留在
"fleur"
直到你松开按钮
Then you don't need the <B3-Motion>
event, the cursor just stays "fleur"
until you release the button
在您发布的代码中,您需要将所有提到的 canevas
替换为 self.canevas
以便能够在 move_start中引用它代码> 函数(以及任何其他类方法)
In the code you posted, you need to replace every mention of canevas
to self.canevas
to be able to reference to it in the move_start
function (and any other class methods)
这篇关于执行鼠标事件时更改光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!