zoukankan      html  css  js  c++  java
  • Centering window on the screen

    The following script shows how we can center a window on the desktop screen.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    """
    ZetCode PyQt4 tutorial 
    
    This program centers a window 
    on the screen. 
    
    author: Jan Bodnar
    website: zetcode.com 
    last edited: October 2011
    """
    
    import sys
    from PyQt4 import QtGui
    
    
    class Example(QtGui.QWidget):
        
        def __init__(self):
            super(Example, self).__init__()
            
            self.initUI()
            
        def initUI(self):               
            
            self.resize(250, 150)
            self.center()
            
            self.setWindowTitle('Center')    
            self.show()
            
        def center(self):
            
            qr = self.frameGeometry()
            cp = QtGui.QDesktopWidget().availableGeometry().center()
            qr.moveCenter(cp)
            self.move(qr.topLeft())
            
            
    def main():
        
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()     
    

    The QtGui.QDesktopWidget class provides information about the user's desktop, including the screen size.

    self.center()
    

    The code that will center the window is placed in the custom center() method.

    qr = self.frameGeometry()
    

    We get a rectangle specifying the geometry of the main window. This includes any window frame.

    cp = QtGui.QDesktopWidget().availableGeometry().center()
    

    We figure out the screen resolution of our monitor. And from this resolution, we get the center point.

    qr.moveCenter(cp)
    

    Our rectangle has already its width and height. Now we set the center of the rectangle to the center of the screen. The rectangle's size is unchanged.

    self.move(qr.topLeft())
    

    We move the top-left point of the application window to the top-left point of the qr rectangle, thus centering the window on our screen.

  • 相关阅读:
    第二阶段冲刺总结09
    第二阶段冲刺总结08
    第二阶段冲刺总结07
    51nod 1799 二分答案(分块打表)
    51nod 1574 排列转换(贪心+鸽巢原理)
    Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)
    Codeforces 627D Preorder Test(二分+树形DP)
    BZOJ 2427 软件安装(强连通分量+树形背包)
    BZOJ 2467 生成树(组合数学)
    BZOJ 2462 矩阵模板(二维hash)
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4435403.html
Copyright © 2011-2022 走看看