1

I am having a problem with Tkinter detecting the wrong cursor position when the application is in fullscreen mode on macOS.

I have created a Python 3 application with a Tkinter GUI on macOS (Mojave). The application works well until the green full screen button is pressed. After this the menu bar is hidden as expected, but the window only expands to 2560x1395 on a 2560x1440 display. The interface also detects the mouse cursor above where it appears on the screen, which makes the application impossible to use. I have tried forcing the application to expand to the full height of 1440, but it immediately changes back to 1395.

Here is a minimal case demonstrating the problem:

import tkinter as tk

def test():
    print("Test")

root = tk.Tk()
tk.Button(root, text="Test", width=20, command=test).pack()
root.mainloop()

While in normal windowed mode, clicking the button causes "Test" to be printed. While in full screen the user must click below the button for the click to be registered. Exiting the application while in full screen mode also causes a segmentation fault.

In my application, clicking where the red dot is causes the OptionMenu to open: Clicking where the red dot is causes the menu to open

jonbush
  • 56
  • 4
  • This [answer](https://stackoverflow.com/a/44329688/10364425) might solve your problem. – Saad May 27 '19 at 04:52
  • Unfortunately answer from Mike - SMT does not help with this issue. – jonbush May 27 '19 at 18:39
  • Are you sure? because I tried it and that fixes your issue for me, I'm on mac as well. Maybe try this `root.attributes("-fullscreen", True)` for fullscreen. – Saad May 27 '19 at 19:35
  • That does make the application go fullscreen and the cursor alignment is correct, but the title bar no longer appears when you move the cursor to the top. This also prevents exiting fullscreen. – jonbush May 28 '19 at 00:15

2 Answers2

0

The applications which support macOS properly opens in a separate Desktop tab when used in fullscreen. As far as I observed this is not case with tkinter, the window expands to fit completely with the screen size onto the main Desktop tab.

Why there is an offset when in fullscreen mode?

The offset is of the titlebar which hides in fullscreen mode but doesn't register and shifts all the widgets accordingly. That's why mouse clicks register + 24 (height of the titlebar) from the click. To avoid this we use root.attributes('-fullscreen', 1).


To exit fullscreen we can use <Escape> bind as Escape key in most application is to exit from fullscreen like in Safari. You can also use a Button.

root.bind('<Escape>', lambda e: root.attributes('-fullscreen', 0))

I couldn't find an event handler for fullscreen mode on Mac so I used <Configure> with condition root.attributes('-fullscreen') to check if the user clicked for fullscreen or not.

This is what I come up with.

from tkinter import *

root = Tk()
Button(root, width=20, text='Hello').pack()

def full_screen(evt=None):
    # checks if the window is in fullscreen
    if root.attributes('-fullscreen'):  
        root.attributes('-fullscreen',1)
        # Remove the borders and titlebar
        root.overrideredirect(True) 
        root.overrideredirect(False)

root.bind('<Configure>', full_screen)
# Escape bind to exit fullscreen.
root.bind('<Escape>', lambda e: root.attributes('-fullscreen', 0))
root.mainloop()
Saad
  • 3,340
  • 2
  • 10
  • 32
  • 1
    Thank you for your help. This still does not solve the issue with python 3.6. However I installed python 3.7.3 and the window maximizes properly when the green button is pressed or Enter Full Screen is selected (but does not enter fullscreen). Since I don't need true fullscreen, this is suitable for the now. It also appears the root.attributes('-fullscreen') is always False, independent of the Python version. – jonbush May 28 '19 at 19:31
  • @jonbush: I'm on python 3.6.8 anaconda it works fine I also tried it on python 3.7.2 and 3.6.8 homebrew. If you really want to get native fullscreen of mac then you need python version bundled with Tk/Tcl 8.5. As I believe most of the python packages are bundled with Tk/Tcl 8.6 which doesn't quite support macOS resources properly. To know more about Tk/Tcl read this [answer](https://stackoverflow.com/a/55516222/10364425) . – Saad May 28 '19 at 19:46
0

This issue is known for 6 months now, and Apple is doing nothing about it, now the fun fact is the logic display is fine (if you record your screen or if you screen share it doesn't happen) it only happens on the physical screen, and it only happens on MBP 16"

I am now over the fact that there is a hardware issue with the (some) MBP 16" video cards and Apple is just playing dumb.

If it helps, having your screen without scale or with "double" scale works fine, it's in the 3 intermediate scales that the issue presents itself

Pedro Borges
  • 1,568
  • 1
  • 14
  • 25