2

I would like to detect if user has clicked the 'maximize' button:

Maximize Button

In tkInter of course, but I don't know how.

I have tried searching through StackOverflow, The Web & tkInter documents(mostly effbot's tkinterbook), but have not found anything related to what I am trying to get.

Nouman
  • 6,947
  • 7
  • 32
  • 60
SF12 Study
  • 375
  • 4
  • 18

1 Answers1

4

There is a good way to does it using .bind(), so let's get started!

As we know, we can maximize the window using the command .state('zoomed').

root.state('zoomed')

And we can get whatever window event by .bind("<Configure>", my_function)

So we can create create a simple function that catches a maximize window event, not necessarily a event by click, but it works.

Here's an example:

import tkinter

def window_event(event):
    if root.state() == 'zoomed':
        print("My window is maximized")

if __name__ == '__main__':

    root = tkinter.Tk()
    root.title("Maximized")

    root.bind("<Configure>", window_event)

    root.mainloop()

EDIT 1: New functionality

import tkinter

def window_event(event):

    if root.state() == 'zoomed':
        print("My window is maximized")

    #GET A NORMAL WINDOW EVENT
    #You can easily do this by a conditional statement, but remember if you even move the window position,
    #this conditional statement will be true, because move the window is a window event
    if root.state() == 'normal':
        print("My window is normal")

if __name__ == '__main__':

    root = tkinter.Tk()
    root.title("Window")
    root.geometry("620x480")

    root.bind("<Configure>", window_event)

    root.mainloop()

EDIT 2: New functionality

import tkinter

count = 0

def window_event(event):
    global count 

    if root.state() == 'zoomed':
        print("My window is maximized")
        count = 0

    if root.state() == 'normal' and count < 1:
        print("My window is normal")
        count +=1

if __name__ == '__main__':

    root = tkinter.Tk()
    root.title("Window")
    root.geometry("620x480")

    root.bind("<Configure>", window_event)

    root.mainloop()

Take a look at this links, they are another way to work with Python GUI:

Tuna
  • 181
  • 8
  • Can you update the answer so it includes for Minimization? – SF12 Study Aug 18 '19 at 07:37
  • When it comes to minimization it's a little bit different because you can't get the `.state()`, let me be more clear. The `root.state('iconic')` minimizes your window, but you can minimize it using the method `.iconify()` as well, so now you can get the `root.state() == iconic`, in other words you can only get the `root.state() == iconic` if you minimize your window by code using the `root.iconify()`, you can read more about it in [iconify()](http://effbot.org/tkinterbook/wm.htm#Tkinter.Wm.iconify-method). Obs.: If you used the `.iconify()` you can also redraw your window by `.deiconify()`. – Tuna Aug 18 '19 at 10:54
  • Thanks for the explanation, but I actually mean when you de-maximize(or make the window normal size) the window. – SF12 Study Aug 20 '19 at 07:54
  • Oh, great! that's possible, I'll add a new functionality for the function `window_event(event)` that dispares a action when the window becomes a normal window. – Tuna Aug 20 '19 at 11:00
  • Sorry for the late reply, it works(I will accept answer), but every time you drag the window, it prints out "My window is normal", is there a way to fix that? – SF12 Study Aug 23 '19 at 10:52
  • Relax, no problem! It's difficult to fix that, because the `` gets all window events and the `.state('normal')` are all window events. My tip for you is: maybe there is other way to solve this problem, but you should give us more detals about what you want to do. – Tuna Aug 23 '19 at 22:14
  • If there is a way to fix it I will try the option, but other than that, I'm fine with your answer! – SF12 Study Aug 24 '19 at 03:16
  • I have an idea, I know how to easily solve this problem by a count, I'll edit the post and add the new functionality. – Tuna Aug 24 '19 at 11:58
  • Sorry for the late reply, but Thank you very much! – SF12 Study Sep 04 '19 at 07:48