【发布时间】:2018-12-24 11:23:32
【问题描述】:
在带有 Python 3.7 的 tkinter 中,事件绑定的默认行为是“
from tkinter import *
class App:
def __init__(self):
self.root = Tk()
# The name kwarg is used to infer the index of the row in the event handlers.
self.labels = [Label(text=f"Label #{i}", name=f"row-{i}") for i in range(5)]
for row, label in enumerate(self.labels):
label.bind("<Button-1>", self.mouse_down)
label.bind("<ButtonRelease-1>", self.mouse_up)
label.bind("<Enter>", self.mouse_enter)
label.grid(row=row, column=0)
mainloop()
def mouse_up(self, event):
idx = self.index_from_event(event)
# Do some with the row with the passed index
def mouse_down(self, event):
idx = self.index_from_event(event)
# Do some with the row with the passed index
def mouse_enter(self, event):
# I would like for this to be triggered even when the mouse is pressed down.
# However, by default tkinter doesn't allow this.
pass
def index_from_event(self, event):
# Get the index of the row from the labels name string.
return str(event.widget).split('-')[-1]
在 tkinter 中按住鼠标按钮 1 时,有什么方法可以启用鼠标输入事件? effbot (http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm) 上的所有文档都提到了 enter 事件:
<Enter>
The mouse pointer entered the widget (this event doesn’t mean that the user pressed the Enter key!).
【问题讨论】:
-
您可以使用
<B1-Motion>并使用事件包含的 (x, y) 坐标吗? -
我不明白你在做什么。 “可滚动”部分从何而来?
-
@Novel 我的示例代码中没有实现滚动逻辑,因为它与问题无关。
标签: python python-3.x tkinter