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

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. ZetCode PyQt4 tutorial
  6.  
  7. This program centers a window
  8. on the screen.
  9.  
  10. author: Jan Bodnar
  11. website: zetcode.com
  12. last edited: October 2011
  13. """
  14.  
  15. import sys
  16. from PyQt4 import QtGui
  17.  
  18. class Example(QtGui.QWidget):
  19.  
  20. def __init__(self):
  21. super(Example, self).__init__()
  22.  
  23. self.initUI()
  24.  
  25. def initUI(self):
  26.  
  27. self.resize(250, 150)
  28. self.center()
  29.  
  30. self.setWindowTitle('Center')
  31. self.show()
  32.  
  33. def center(self):
  34.  
  35. qr = self.frameGeometry()
  36. cp = QtGui.QDesktopWidget().availableGeometry().center()
  37. qr.moveCenter(cp)
  38. self.move(qr.topLeft())
  39.  
  40. def main():
  41.  
  42. app = QtGui.QApplication(sys.argv)
  43. ex = Example()
  44. sys.exit(app.exec_())
  45.  
  46. if __name__ == '__main__':
  47. main()

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

  1. self.center()

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

  1. qr = self.frameGeometry()

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

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

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

  1. 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.

  1. 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.

Centering window on the screen的更多相关文章

  1. 《JAVASCRIPT高级程序设计》window/location/navigator/screen/history对象

    如果要在web中使用JAVASCRIPT,那么BOM(浏览器对象模型)毫无疑问是最重要的部分.BOM提供了很多对象,例如,window.location.navigator.screen.histor ...

  2. JS BOM基础 全局对象 window location history screen navigator

    全局变量声明的两种方式:1,window.变量名=值;2,var 变量名=值; 全局函数声明的两种方式:1,window.函数名=function(){}2,function 函数名=function ...

  3. Quickly place a window to another screen using only the keyboard

    http://askubuntu.com/questions/22207/quickly-place-a-window-to-another-screen-using-only-the-keyboar ...

  4. window对象的screen详解

    screen.availHeight     返回屏幕的高度(不包括Windows任务栏)screen.availWidth     返回屏幕的宽度(不包括Windows任务栏)screen.colo ...

  5. JavaScript之BOM五大对象(window;location;navigator;screen;history)

    一.window 1.浏览器窗口与页面各部分尺寸 2.间歇调用与超时调用 setInterval(function,time(以毫秒计)) 返回:时间Id号(为clearInterval(timeId ...

  6. [browser window窗口大小 算是screen补充吧]主要因为移动IE游览器 写了个兼容

    先上图吧 来上代码 console.log(window.outerWidth + '--' + window.outerHeight);//只读的整数,声明了整个窗口的XY //IE 不支持此属性, ...

  7. [虾扯蛋] android界面框架-Window

    从纯sdk及framwork的角度看,android中界面框架相关的类型有:Window,WindowManager,View等.下面就以这几个类为出发点来概览下安卓开发的"界面架构&quo ...

  8. JavaScript权威设计--Window对象(简要学习笔记十三)

    1.Window对象是所有客户端JavaScript特性和API的主要接入点. Window对象中的一个重要属性是document,它引用Document对象. JavaScript程序可以通过Doc ...

  9. Navigator对象、Screen对象

    Navigator对象:         Window对象的navigator属性引用的是包含浏览器厂商和版本信息的Navigator对象:   Navigator对象集合:plugins[] 返回对 ...

随机推荐

  1. IO流-递归遍历目录下指定后缀名结尾的文件名称

    /* *自定义遍历目录下指定后缀名结尾文件的名称的方法: * * param file:指定目录 name:指定后缀名 */ 1 public static void FileName(File fi ...

  2. HeapAlloc 和 GlobalAlloc 以及 VirtualAlloc 三者之间的关系(转)

    VirtualAlloc 一次分配 1PAGE 以上的 RAM. 每次分配都是 PAGE 的整数倍. 你不会想为了分配 1 个 BYTE 的空间而浪费剩下的 4095 字节. OK, 你可以自己写算法 ...

  3. HDU 4727 The Number Off of FFF (水题)

    The Number Off of FFF Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. JS实现《黑客帝国》落地字母背景

    JS实现<黑客帝国>落地字母背景.这个特别有意思,主要是通过设置字符相关属性.控制循环字母距离顶部的高度值,来达到字母不断循环下落的功能. 恩,还有加上一个随机机制,出现各种大小 各个位置 ...

  5. [html5]使用localStorage兼容低版本Safari无法使用indexeddb的情况

    摘要 简单场景描述:将html5开发的app内嵌入ios app中,有部分数据,需要在本地存储,就想到使用浏览器的localstorage或者indexeddb,另外localstorage存储的方式 ...

  6. fundamentals5

    PROTOBUF的DELPHI开源框架fundamentals5 GITHUB: https://github.com/fundamentalslib/fundamentals5 # Fundamen ...

  7. android软件中加入广告实现方法

    经过了一番折腾,忙忙碌碌了一下午,终于搞明白了Android软件界面嵌入广告的方法,以下我以嵌入有米广告为例小结一下: 步骤一,下载有米广告SDK,将 youmi-android.jar 导入想要嵌入 ...

  8. 【spring cloud】【docker】使用docker在centOS上部署spring cloud微服务架构服务

    项目GitHub地址 ================================================================================== 部署过程: ...

  9. Gradle在Windows环境与Linux上配置有哪些不同?

    我的开发环境:Windows + Android Studio + Gradle 2.8 all + Jenkins 公司CI 服务器环境: Linux + Gradle 2.10 bin + Jen ...

  10. 新手学习selenium路线图(老司机亲手绘制)-学前篇

    前言: 本来这篇是只在微信公众号(yoyoketang)上发布的,最近一搜,发现本很多人copy(copy公众号的,图片是加载不出来的)了,还没注明出处,不想多说什么,博客上也同步更新这篇吧! 最近群 ...