Would you like to center your Pyside2 or Pyside6 window? Here is the code how to do it.

pyside6 Center Window.

center window

The code to center a window in pyside is as follows:

center = QScreen.availableGeometry(QApplication.primaryScreen()).center()
geo = mainwindow.frameGeometry()
geo.moveCenter(center)
mainwindow.move(geo.topLeft())

Full example

import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtWidgets import QMainWindow
from PySide6.QtGui import QScreen

app = QApplication(sys.argv)
mainwindow = QMainWindow()
mainwindow.setGeometry(0, 0, 800, 600)
mainwindow.show()

center = QScreen.availableGeometry(QApplication.primaryScreen()).center()
geo = mainwindow.frameGeometry()
geo.moveCenter(center)
mainwindow.move(geo.topLeft())

app.exec_()

And here is the result:

center window

Written by Loek van den Ouweland on 2021-01-06.
Questions regarding this artice? You can send them to the address below.