Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can center a window in PySide6 by using the QDesktopWidget class to get the size of the screen and then centering your window on it.

Here's an example:

from PySide6.QtGui import QDesktopWidget
from PySide6.QtWidgets import QApplication, QMainWindow

app = QApplication([])
window = QMainWindow()

# Set window dimensions
window.resize(800, 600)

# Center window on screen
desktop = QDesktopWidget()
rect = desktop.availableGeometry(desktop.primaryScreen())
x = int((rect.width() - window.width()) / 2)
y = int((rect.height() - window.height()) / 2)
window.move(x, y)

window.show()
app.exec_()

This should work regardless of whether you're using QT, QML, or WSL2.