简体   繁体   English

在Tkinter中的最小化窗口上运行命令

[英]Running a command on Window minimization in Tkinter

I have a Tkinter window whenever the minimize button is pressed I'd like to run a command, how do I do this? 每当我按下最小化按钮时,我都有一个Tkinter窗口,我想运行一个命令,我该怎么做?

I know w.protocol("WM_DELETE_WINDOW", w.command) will run a command on exit. 我知道w.protocol(“ WM_DELETE_WINDOW”,w.command)将在退出时运行命令。

You can bind to the <Unmap> event. 您可以绑定到<Unmap>事件。

For example, run the following code and then minimize the main window. 例如,运行以下代码,然后最小化主窗口。 The tool window should disappear when the main window is minimized. 当主窗口最小化时,工具窗口应消失。

import Tkinter as tk

class App:
    def __init__(self):
        self.root = tk.Tk()
        tk.Label(self.root, text="main window").pack()
        self.t = tk.Toplevel()
        tk.Label(self.t, text="tool window").pack()
        self.root.bind("<Unmap>", self.OnUnmap)
        self.root.bind("<Map>", self.OnMap)
        self.root.mainloop()

    def OnMap(self, event):
        # show the tool window
        self.t.wm_deiconify()

    def OnUnmap(self, event):
        # withdraw the tool window
        self.t.wm_withdraw()

if __name__ == "__main__":
    app=App()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM