These advertisements helps me to keep writing articles on this website

pyside2 on macOS Big Sur does not show the main window.

Are you using pyside2 but your main window is not showing? Here are two ways to fix that.

A python GUI project made with pyside2 on macOS Big Sur sometimes does not show the main window. You can see the icon in the tray:

icon

But you see no window and have to force quit the app. The problem can be solved by adding an environment variable.

Solution 1

Put this before creating the main window:

import os
os.environ['QT_MAC_WANTS_LAYER'] = '1'

Full example

import sys
from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QLabel

import os
os.environ['QT_MAC_WANTS_LAYER'] = '1'

app = QApplication(sys.argv)
label = QLabel("<center><h1>I must have left my house at eight,<br />because I always do.</h1></center>")
label.show()
app.exec_()

And boom! The window is shown:

window

Solution 2: Use pyside6

In pyside6 the Layer-backing is always enabled. If you add the code os.environ['QT_MAC_WANTS_LAYER'] = '1' in a pyside6 project, you get the following warning:

Layer-backing is always enabled.  QT_MAC_WANTS_LAYER/_q_mac_wantsLayer has no effect.

Conclusion

Add the line os.environ['QT_MAC_WANTS_LAYER'] = '1' in a pyside2 project or upgrade to pyside6.

Written by Loek van den Ouweland on January 06, 2021. Questions regarding this artice? You can send them to the address below.
By using this site, you acknowledge that you have read and understand our Cookie and Privacy Policy.