如何使frame<Leave>事件考虑所有子窗口小部件?

2024-05-16 02:03:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码显示一个弹出窗口(无装饰),该窗口在LabelFrame中显示项目列表框及其计数

由于这是一个弹出窗口,我的意图是在光标移出窗口时隐藏该窗口

使用下面的选项,当鼠标光标移离帧时,窗口将消失,但当鼠标移到Scrollbar上时,窗口也将消失

出于某种原因,将<Leave>绑定到对象本身时也会考虑Listbox,而不考虑Scrollbar

为什么??如何修复代码,使鼠标悬停在框架的所有小部件上时框架可见,并且只有当它移出整个框架时,它才会消失

class ForeachViewer(Toplevel):
    def __init__(self, parent):
        Toplevel.__init__(self, parent)

        self.overrideredirect(True)
        self.main_frame = LabelFrame(self, text="Total:")
        self.main_frame.pack(expand=1, fill='both', padx=5, pady=5)

        self.content_items = StringVar()
        self.group_expansion = Listbox(self.main_frame,
                                       activestyle = 'none',
                                       selectmode='single',
                                       font=text_font,
                                       listvariable = self.content_items,
                                       relief='flat',
                                       highlightthickness = 0)
        self.group_expansion.grid(row=1, column=1, sticky="nswe")

        self.yscrollbar = Scrollbar(self.main_frame, orient='vertical')
        self.yscrollbar.grid(row=1, column=2, sticky='ns')

        self.group_expansion.configure(yscrollcommand=self.yscrollbar.set)
        self.yscrollbar.configure(command=self.group_expansion.yview)

        self.bind("<Leave>", lambda event : self.hide())
        self.hide()

    def contents(self, content):
        max_length = len(max(content, key=len)) + 5
        self.main_frame.configure(text= " Total Items: %d" % len(content))
        self.group_expansion.configure(state="normal", width=max_length)
        self.content_items.set(" ".join(content))
        self.group_expansion.configure(state="disable")

    def show(self, at_x, at_y):
        self.geometry("+%d+%d" % (at_x, at_y))
        self.deiconify()

    def hide(self):
        self.withdraw()

Tags: textself框架mainconfiguredefgroupitems