'PyQt4 center window on active screen
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!
Solution 1:[1]
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())
Solution 2:[2]
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())
Solution 3:[3]
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())
Solution 4:[4]
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.
Solution 5:[5]
self.move(QDesktopWidget().availableGeometry().center().x() - self.frameGeometry().center().x() * 0.5, QDesktopWidget().availableGeometry().center().y() - self.frameGeometry().center().y() * 0.5)
Solution 6:[6]
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)
Solution 7:[7]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | NL23codes |
| Solution 2 | Pedro Lobito |
| Solution 3 | Sergio Montazzolli |
| Solution 4 | nonimportant |
| Solution 5 | chirag sanghvi |
| Solution 6 | Darkavid |
| Solution 7 | abubasil |
