18

How I can center window on active screen but not on general screen? This code moves window to center on general screen, not active screen:

import sys
from PyQt4 import QtGui

class MainWindow(QtGui.QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.initUI()

    def initUI(self):

        self.resize(640, 480)
        self.setWindowTitle('Backlight management')
        self.center()

        self.show()

    def center(self):
        frameGm = self.frameGeometry()
        centerPoint = QtGui.QDesktopWidget().availableGeometry().center()
        frameGm.moveCenter(centerPoint)
        self.move(frameGm.topLeft())

def main():
    app = QtGui.QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

If I removes self.center() from initUI() then window opened on 0x0 on active screen. I need to open window on active screen and move this window on center of this screen. Thansk!

Applejohn
  • 315
  • 1
  • 2
  • 5

8 Answers8

31

Modify your center method to be as follows:

def center(self):
    frameGm = self.frameGeometry()
    screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos())
    centerPoint = QtGui.QApplication.desktop().screenGeometry(screen).center()
    frameGm.moveCenter(centerPoint)
    self.move(frameGm.topLeft())

This function is based on where the mouse point is located. It uses the screenNumber function to determine which screen the mouse is current active on. It then finds the screenGeometry of that monitor and the center point of that screen. Using this method, you should be able to place the window in the center of a screen even if monitor resolutions are different.

Andy
  • 49,085
  • 60
  • 166
  • 233
  • Note this didn't centre the screen properly for me without the call to `resize()` first – mchen Jul 13 '23 at 19:24
8

One correction for PyQt5 users:

import PyQt5

def center(self):
    frameGm = self.frameGeometry()
    screen = PyQt5.QtWidgets.QApplication.desktop().screenNumber(PyQt5.QtWidgets.QApplication.desktop().cursor().pos())
    centerPoint = PyQt5.QtWidgets.QApplication.desktop().screenGeometry(screen).center()
    frameGm.moveCenter(centerPoint)
    self.move(frameGm.topLeft())
NL23codes
  • 1,181
  • 1
  • 14
  • 31
3

Yet another (late) answer:

from PyQt5.QtWidgets import QDesktopWidget

def center(self):
    qr = self.frameGeometry()
    cp = QDesktopWidget().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
1

The other answers worked with a few deprecated warnings when using PySide2. So here is my version of the same function:

def center(self):
    screen = QtGui.QGuiApplication.screenAt(QtGui.QCursor().pos())
    fg = self.frameGeometry()
    fg.moveCenter(screen.geometry().center())
    self.move(fg.topLeft())
1

This is the update that works with pyside6:

def center(self):
    frame_geo = self.frameGeometry()
    screen = self.window().windowHandle().screen()
    center_loc = screen.geometry().center()
    frame_geo.moveCenter(center_loc)
    self.move(frame_geo.topLeft())

It will move the window to the center of the screen where it is located.

nonimportant
  • 406
  • 4
  • 15
Jay
  • 56
  • 4
0
self.move(QDesktopWidget().availableGeometry().center().x() - self.frameGeometry().center().x() * 0.5, QDesktopWidget().availableGeometry().center().y() - self.frameGeometry().center().y() * 0.5)
chirag sanghvi
  • 652
  • 6
  • 8
0

None of the above worked for me, so I had to come up with my own solution.

This works with PyQt5 and for multiple monitors.

For different window dimensions just play with the multipliers:

from PyQt5 import QtWidgets

main_window = QtWidgets.QMainWindow()
multiplier_x = 2
multiplier_y = 2
cursor_pos = QtWidgets.QApplication.desktop().cursor().pos()
screen = QtWidgets.QApplication.desktop().screenNumber(cursor_pos)
pos_x = QtWidgets.QDesktopWidget().screenGeometry(screen).center().x()
pos_x -= main_window.frameGeometry().center().x() * multiplier_x
pos_y = QtWidgets.QDesktopWidget().screenGeometry(screen).center().y()
pos_y -= main_window.frameGeometry().center().y() * multiplier_y
main_window.move(pos_x, pos_y)
0
frameGeometry = self.frameGeometry()
windowFrameGeometry = self.window.frameGeometry()
x = int(frameGeometry.center().x()-windowFrameGeometry.center().x())
y = int(frameGeometry.center().y()-windowFrameGeometry.center().y())
self.window.move(x,y)
abubasil
  • 11
  • 2