鼠标按钮 DownPress 的 Tkinter 事件(按住)?
Tkinter event for DownPress of mouse button (holding down)?
更新:这似乎是一个版本问题。 python 3.6.1 的事件不会在点击时触发,但可以在我目前测试过的 2.7 上运行。
更新:Bryan 的回答确实解决了我的事件无法正常工作的问题,但是在我的 3.6.1 版本 python 上,事件没有在按下时触发无效的问题仍然是一个问题.
Python版本=3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
我正在尝试编写一个事件处理程序,以便在按住鼠标按钮时重复执行某些操作。我一直在搜索文档和互联网,但我找不到任何关于按住鼠标左键的参考。
是否有专门针对按住鼠标左键的事件?有一个 even for release <ButtonRelease-1>
但没有 seam 是一个 click and hold 事件。
我已经尝试了 <Button-1>
及其所有同义事件以防万一,但没有成功。该事件仅在发布时触发,但不会像我想要的那样在按下时触发。我什至不需要持续按住按钮的事件,我只需要一个 even 来按下按钮。
欢迎提供文档,因为我找不到文档。
更新:
这是一个示例代码。它只会在释放按钮时打印。请记住,我正在尝试按下滚动条上的箭头按钮并更改滚动速度。
滚动条处理滚动箭头的方式是否不同于按钮?
import tkinter as tk
root = tk.Tk()
textbox = tk.Text(root, height = 10)
textbox.grid(row=0, column=0)
scrolling = False
yscroll = tk.Scrollbar(root, command=textbox.yview,orient="vertical", repeatinterval=10, repeatdelay=30)
textbox.configure(yscrollcommand = yscroll.set)
yscroll.grid(row=0, column=1, sticky="ns")
for i in range(1000):
textbox.insert(tk.END, "{}\n".format(i))
def scrolling_active(arrow):
global scrolling
root.bind('<ButtonRelease-1>', stop_scrolling())
print(arrow)
if scrolling == True:
if arrow == "arrow1":
textbox.tk.call(textbox._w,'yview', 'scroll', -100, 'units')
if arrow == "arrow2":
textbox.tk.call(textbox._w,'yview', 'scroll', 100, 'units')
root.after(100, lambda a = arrow: scrolling_active(a))
def start_scrolling(event):
global scrolling
scrolling = True
scrolling_active(yscroll.identify(event.x, event.y))
def stop_scrolling():
global scrolling
scrolling = False
yscroll.bind("<Button-1>", start_scrolling)
root.mainloop()
您应该能够绑定到 <ButtonPress-1>
以在最初单击鼠标时使用,然后调用重复函数直到 <ButtonRelease-1>
停止它,如您所述。
没有按住按钮的事件。只有按下和释放按钮的事件。
如果您需要在按钮按下时进行跟踪,您可以在按下时设置一个标志并在释放时取消设置。
您的代码中有一个错误。考虑这段代码:
root.bind('<ButtonRelease-1>', stop_scrolling())
它在功能上与此相同:
result=stop_scrolling()
root.bind('<ButtonRelease-1>`, None)
要进行绑定,您必须删除 ()
:
root.bind('<ButtonRelease-1>', stop_scrolling)
您还需要stop_scrolling
接受事件对象:
def stop_scrolling(event):
global scrolling
scrolling = False
也不需要调用textbox.tk.call
。您可以在 tkinter 对象 (textbox.yview("scroll", -100, "units"
)
上调用 yview
方法
我知道有点晚了,但我想做完全相同的事情(在 canvas 中拖动图像以制作操纵杆控制器)。
阅读文档 Events and Bindings 我发现了这个事件:'<B1-Motion>'
这是我做的工作示例:
from tkinter import *
root = Tk()
root.geometry('800x600')
image1 = PhotoImage(file = 'images/button.png')
x_img,y_img = 250,250
canvas = Canvas(root, width = 500, height = 400, bg = 'white')
canvas.pack()
imageFinal = canvas.create_image(x_img, y_img, image = image1)
def move(event):
global x_img,y_img
x, y = event.x, event.y
print('{}, {}'.format(x, y))
canvas.move(imageFinal, x-x_img,y-y_img)
x_img = x
y_img = y
canvas.update()
canvas.bind('<B1-Motion>', move)
root.mainloop()
更新:这似乎是一个版本问题。 python 3.6.1 的事件不会在点击时触发,但可以在我目前测试过的 2.7 上运行。
更新:Bryan 的回答确实解决了我的事件无法正常工作的问题,但是在我的 3.6.1 版本 python 上,事件没有在按下时触发无效的问题仍然是一个问题.
Python版本=3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
我正在尝试编写一个事件处理程序,以便在按住鼠标按钮时重复执行某些操作。我一直在搜索文档和互联网,但我找不到任何关于按住鼠标左键的参考。
是否有专门针对按住鼠标左键的事件?有一个 even for release <ButtonRelease-1>
但没有 seam 是一个 click and hold 事件。
我已经尝试了 <Button-1>
及其所有同义事件以防万一,但没有成功。该事件仅在发布时触发,但不会像我想要的那样在按下时触发。我什至不需要持续按住按钮的事件,我只需要一个 even 来按下按钮。
欢迎提供文档,因为我找不到文档。
更新:
这是一个示例代码。它只会在释放按钮时打印。请记住,我正在尝试按下滚动条上的箭头按钮并更改滚动速度。
滚动条处理滚动箭头的方式是否不同于按钮?
import tkinter as tk
root = tk.Tk()
textbox = tk.Text(root, height = 10)
textbox.grid(row=0, column=0)
scrolling = False
yscroll = tk.Scrollbar(root, command=textbox.yview,orient="vertical", repeatinterval=10, repeatdelay=30)
textbox.configure(yscrollcommand = yscroll.set)
yscroll.grid(row=0, column=1, sticky="ns")
for i in range(1000):
textbox.insert(tk.END, "{}\n".format(i))
def scrolling_active(arrow):
global scrolling
root.bind('<ButtonRelease-1>', stop_scrolling())
print(arrow)
if scrolling == True:
if arrow == "arrow1":
textbox.tk.call(textbox._w,'yview', 'scroll', -100, 'units')
if arrow == "arrow2":
textbox.tk.call(textbox._w,'yview', 'scroll', 100, 'units')
root.after(100, lambda a = arrow: scrolling_active(a))
def start_scrolling(event):
global scrolling
scrolling = True
scrolling_active(yscroll.identify(event.x, event.y))
def stop_scrolling():
global scrolling
scrolling = False
yscroll.bind("<Button-1>", start_scrolling)
root.mainloop()
您应该能够绑定到 <ButtonPress-1>
以在最初单击鼠标时使用,然后调用重复函数直到 <ButtonRelease-1>
停止它,如您所述。
没有按住按钮的事件。只有按下和释放按钮的事件。
如果您需要在按钮按下时进行跟踪,您可以在按下时设置一个标志并在释放时取消设置。
您的代码中有一个错误。考虑这段代码:
root.bind('<ButtonRelease-1>', stop_scrolling())
它在功能上与此相同:
result=stop_scrolling()
root.bind('<ButtonRelease-1>`, None)
要进行绑定,您必须删除 ()
:
root.bind('<ButtonRelease-1>', stop_scrolling)
您还需要stop_scrolling
接受事件对象:
def stop_scrolling(event):
global scrolling
scrolling = False
也不需要调用textbox.tk.call
。您可以在 tkinter 对象 (textbox.yview("scroll", -100, "units"
)
yview
方法
我知道有点晚了,但我想做完全相同的事情(在 canvas 中拖动图像以制作操纵杆控制器)。
阅读文档 Events and Bindings 我发现了这个事件:'<B1-Motion>'
这是我做的工作示例:
from tkinter import *
root = Tk()
root.geometry('800x600')
image1 = PhotoImage(file = 'images/button.png')
x_img,y_img = 250,250
canvas = Canvas(root, width = 500, height = 400, bg = 'white')
canvas.pack()
imageFinal = canvas.create_image(x_img, y_img, image = image1)
def move(event):
global x_img,y_img
x, y = event.x, event.y
print('{}, {}'.format(x, y))
canvas.move(imageFinal, x-x_img,y-y_img)
x_img = x
y_img = y
canvas.update()
canvas.bind('<B1-Motion>', move)
root.mainloop()