Detect if user has clicked the 'maximized' button

梦想与她 提交于 2021-02-10 14:18:38

问题


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.


回答1:


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:

  • PyGUI
  • PySimpleGUI


来源:https://stackoverflow.com/questions/57533458/detect-if-user-has-clicked-the-maximized-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!