Advertisement
eeperry

PyQt: Center main window on screen

Oct 12th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. from PyQt4.QtGui import *
  2. from PyQt4.QtCore import *
  3.  
  4. ## This would go in some a function after the window is created
  5. ##   Perhaps using a one-shot-timer at the end of the windows __init__()
  6. ##        ex: QTimer.singleShot(0, self.centerWindow)
  7.  
  8. # screen geometry
  9. screen = QDesktopWidget().screenGeometry()      
  10.  
  11. # mainwindow geometry
  12. appsize = self.geometry()
  13.  
  14. # horizontal center
  15. hpos = (screen.width() - appsize.width()) / 2
  16.  
  17. # vertical center
  18. vpos = (screen.height() - appsize.height()) / 2
  19.  
  20. # move main window to center
  21. self.move(hpos,  vpos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement