【问题标题】:Tkinter Leave event firing only on program startup?Tkinter Leave 事件仅在程序启动时触发?
【发布时间】:2020-10-07 05:52:16
【问题描述】:

我正在尝试放置一个小标签,提示用户绑定到它的键盘快捷键,效果很好。

当我离开按钮时出现问题。标签消失了,但不完全消失。如果我尝试单击标签“曾经”所在的区域中的按钮,它不会触发,表明它仍然存在。所以我想也许按钮已经内置了一个离开事件,可以将它们提升到顶层。我决定尝试覆盖它,并且基本上告诉按钮在键提示标签hit_button.bind("<Leave>", hit_button.lower(hit_button_keyhint) 下方去(或者更好,只是停留),但无济于事。我做错了什么?

                                       fg=hit_button['fg'])
    hit_button_keyhint.place(relx=0.5, rely=0.85, anchor='center')
    hit_button.bind("<Leave>", hit_button.lower(hit_button_keyhint))

【问题讨论】:

  • 另外,我忘了提一下,使用print("left")我意识到该事件仅在游戏启动时触发一次,但再也不会触发。为什么在我用光标输入按钮之前它会触发?
  • edit您的问题包含minimal reproducible example

标签: button tkinter bind mouseleave


【解决方案1】:

你需要传递一个函数给绑定,这里你执行一个函数。 (hit_button.lower(hit_button_keyhint))

为此,您应该使用 lambda。

你可以在这里得到一些澄清: Tkinter binding a function with arguments to a widget

但这应该可行:

hit_button.bind("<Leave>",lambda event, k=hit_button_keyhint:hit_button.lower(k)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多