【问题标题】:Hide or Close Tkinter Top Level Window When Main Window Is Not Visible当主窗口不可见时隐藏或关闭 Tkinter 顶级窗口
【发布时间】:2021-05-18 16:41:53
【问题描述】:

如果主窗口不可见,有没有办法关闭或最好隐藏顶级窗口。我已经能够通过检查是否单击了最小化按钮来做到这一点,但是有几种方法可以“最小化”一个窗口,例如进入另一个选项卡,点击任务栏的右侧(或左侧取决于您的系统)语言)等...谢谢!

【问题讨论】:

  • 尝试<tkinter.Toplevel>.withdraw()隐藏窗口,<tkinter.Toplevel>.deiconify()显示窗口,<tkinter.Toplevel>.iconnify()强制窗口最小化
  • 是的,但是我该怎么做才能在每次主窗口不可见时运行
  • 你试过绑定"<Map>""<Unmap>"
  • 您也可以绑定到"<FocusOut>""<FocusIn>" 并创建一些逻辑来完成这项工作

标签: python tkinter toplevel


【解决方案1】:

您可以尝试将函数绑定到窗口的<Unmap><Map> 事件。如下:

from tkinter import *


root = Tk()
root.geometry("600x300")
for _ in range(3):
    Toplevel(root)

def hide_all(event):
    # You could also call each child directly if you have the object but since we aren't tracking
    # the toplevels we need to loop throught the children
    for child in root.children.values():
        if isinstance(child, Toplevel):
            child.withdraw() # or depending on the os: 'child.wm_withdraw()'

def show_all(event):
    # You could also call each child directly if you have the object but since we aren't tracking
    # the toplevels we need to loop throught the children
    for child in root.children.values():
        if isinstance(child, Toplevel):
            child.deiconify() # or depending on the os: 'child.wm_deiconify()'

root.bind("<Unmap>", hide_all) # Fires whenever 'root' is minimzed
root.bind("<Map>", show_all) # Fires whenever 'root' is restored and shown
root.mainloop()

如果这不是您想要的,您可以使用&lt;Expose&gt;&lt;Visibility&gt; 事件来获得您想要的。链接到所有 tkinter 事件:tkinter events from anzeljg

【讨论】:

    猜你喜欢
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多