-
-
Save ekapujiw2002/c506aee6f431737440e18442709930c1 to your computer and use it in GitHub Desktop.
[qt5] center a window on screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'tom' | |
import sys | |
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication | |
class Example(QWidget): | |
def __init__(self): | |
super().__init__() | |
self.init_ui() | |
def init_ui(self): | |
self.resize(250, 150) | |
self.center() | |
self.setWindowTitle('Center') | |
self.show() | |
def center(self): | |
# geometry of the main window | |
qr = self.frameGeometry() | |
# center point of screen | |
cp = QDesktopWidget().availableGeometry().center() | |
# move rectangle's center point to screen's center point | |
qr.moveCenter(cp) | |
# top left of rectangle becomes top left of window centering it | |
self.move(qr.topLeft()) | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
ex = Example() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment