To get focus automatically for created window in Tkinter (macOS)

In macOS, windows created by Tkinter do not automatically get focus. This is annoying when using PyCharm. If you do not take any actions, the created window will be hidden behind PyCharm. An alternative is to set the window to the top and then cancel it (code show as below). This will make the window visible after creation.

The above method is only to make the window appear in the front, the color of the title bar is still gray, which means that it is not activated.

Solution A: pyobjc

To install pyobjc module, executing…

During the installation of pyobjc, many other modules will be installed at the same time, in order to uninstall them together. Please execute the following command when you need to remove the whole pyobjc.

Solution B: applescript

This solution is recommended because of no extra module needed.

Leave a Reply

Your email address will not be published. Required fields are marked *