How do I bind an event to the left mouse button being held down?
I need a command to be executed as long as the left mouse butt开发者_StackOverflowon is being held down.
If you want "something to happen" without any intervening events (ie: without the user moving the mouse or pressing any other buttons) your only choice is to poll. Set a flag when the button is pressed, unset it when released. While polling, check the flag and run your code if its set.
Here's something to illustrate the point:
import Tkinter
class App:
def __init__(self, root):
self.root = root
self.mouse_pressed = False
f = Tkinter.Frame(width=100, height=100, background="bisque")
f.pack(padx=100, pady=100)
f.bind("<ButtonPress-1>", self.OnMouseDown)
f.bind("<ButtonRelease-1>", self.OnMouseUp)
def do_work(self):
x = self.root.winfo_pointerx()
y = self.root.winfo_pointery()
print "button is being pressed... %s/%s" % (x, y)
def OnMouseDown(self, event):
self.mouse_pressed = True
self.poll()
def OnMouseUp(self, event):
self.root.after_cancel(self.after_id)
def poll(self):
if self.mouse_pressed:
self.do_work()
self.after_id = self.root.after(250, self.poll)
root=Tkinter.Tk()
app = App(root)
root.mainloop()
However, polling is generally not necessary in a GUI app. You probably only care about what happens while the mouse is pressed and is moving. In that case, instead of the poll function simply bind do_work to a <B1-Motion>
event.
Look at table 7-1 of the docs. There are events that specify motion while the button is pressed, <B1-Motion>
, <B2-Motion>
etc.
If you're not talking about a press-and-move event, then you can start doing your activity on <Button-1>
and stop doing it when you receive <B1-Release>
.
Use the mouse move/motion events and check the modifier flags. The mouse buttons will show up there.
精彩评论