我使用Tkinter / ttk创建了一个稍微复杂的UI,其中包含其他框架中包含的多个框架。
当我从内部框架转到父框架时,我希望触发<Enter>
事件。
我已经在<Leave>
事件上绑定了其他功能,但是如何获取“当前”窗口小部件,以便可以在父窗口小部件上触发“假” <Enter>
事件?但这仅是在将孩子留给父母时,而不是同时离开孩子和父母(例如在边界处)时。
下面是一个代码示例,用以说明我的意思:
#!/usr/bin/python3
import tkinter
from tkinter import ttk
def onEnter(event):
print('Widget: %s', event.widget)
class FrameTop(ttk.LabelFrame):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, text='TopFrame', **kwargs)
for i in range(4):
lbl_t = ttk.Label(self, text='Top Label %d' % i)
lbl_t.pack()
self.bottom = FrameBottom(self, 1)
self.bottom.pack()
self.lbl = ttk.Label(self.bottom, text='Label')
self.lbl.pack()
class FrameBottom(ttk.LabelFrame):
def __init__(self, master, num, *args, **kwargs):
super().__init__(master, *args, text='BottomFrame %d' % num, **kwargs)
for i in range(5):
lbl_t = ttk.Label(self, text='#%d: Label %d' % (num, i))
lbl_t.pack()
if num < 5:
inner = FrameBottom(self, num + 1)
inner.pack()
self.bind('<Enter>', onEnter, add=True) # Bind Bottom
root = tkinter.Tk()
tp = FrameTop(root)
tp.pack()
tp.bind('<Enter>', onEnter, add=True) # Bind Bottom
root.mainloop()