Home > Back-end >  How to handle minimize/maximize buttons in tkinter
How to handle minimize/maximize buttons in tkinter

Time:06-26

I've researched a lot about this topic, but nothing seems to be helpful. I want to get a callback when I press the minimize/maximize (- button) on my tkinter window. Like when I click on the close button, I can get a callback like this:

# Function for callback
def on_closing():
   print("User clicked close button")
   root.destroy()

# Callback
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)

Like this I can callback a function when someone clicks on the close (X) button. So my question is, isn't there a similar protocol for minimize/maximize buttons as well for calling back?

CodePudding user response:

You wont be able to use a protocol like with the X button. You can bind to the '<Map>' and '<Unmap>' events like this.

import tkinter as tk


def catch_minimize(event):
    print("window minimzed")


def catch_maximize(event):
    print("window maximized")

    
root = tk.Tk()
root.geometry("400x400")

root.bind("<Unmap>", catch_minimize)
root.bind("<Map>", catch_maximize)

root.mainloop()
  • Related