Search code examples
pythonpython-3.xtkintertk-toolkitdpi

How to detect in Tkinter that whole window is moved (esspecially to the other screen)?


I want to change scaling depend on screen. I know how to scale but I do not know how to detect that windows is move from screen 1 to screen 2.

What event and bind is need for it?


Solution

  • Basically I have to disappointing you, there is no such binding to detect that. There is however a long history of trying to get a cross platform specific solution working, without fully success. You can read something about it in the tcler's-wiki where I have the basic idea from.

    The only way to track your window by moving is by a binding to '<Configure>', this event is fired when you move the window. Within a event-handler you can track the position with event.root_x and event.root_y. These coordinates are screencoordinates from the window. So far so good, we can know when we are moving and where we are.

    A useful insight is that the first screen, most often the left one, has always the coordinate 0,0 as the upper left corner. Place your window there with window.geometry('+0+0') ensure the window is placed there via window.update_idletasks() ask for wm_maxsize, that is the width and height of the current screen and usually used to zoom your window. The next thing you have to do is a little bit of math against winfo_screenwidth() and winfo_screenheight() that usually gives you all the available space on the screen.

    EDIT: Also try winfo_vrootwidth() and winfo_vrootheight()

    There you have it, you know where the second screen begins and how to track the window movement. I think you are good to go with that and try it out, but keep in mind that this technique is not OS-independent.

    An additional thought that I had was to invoke that procedure in a loop and to search for more screens, but never actual coded it. Pro tip: when you do window.wm_attributes('-alpha', 0) you don't have to watch your window jumping around by gathering the needed information.