These advertisements helps me to keep writing articles on this website

pyside6 Center Window.

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

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 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.