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.

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. Hibernate3 jar包的作用[转]

    from:http://nopainnogain.iteye.com/blog/761630 (1)hibernate3.jar: Hibernate的核心库,没有什么可说的,必须使用的jar包 (2 ...

  2. jquery常用写法简单记录

    好久不写东西了......话不多说,主要记录一下,最近做的项目中用到的js的记录(虽然特别特别简单) 一 jquery常用写法记录 jQuery(this).addClass("select ...

  3. php-curl小记

    用jQuery: $.ajax({ url:url, type:"POST", data:data, contentType:"application/json; cha ...

  4. Toast信息框

    Toast组件的功能和对话框有些相似,可是使用上更简单,使用Toast组件的目的仅仅有一个,就是在屏幕上弹出一个消息窗体告知用户某个信息,并且这个窗体没有不论什么button,经过几秒钟后就会消失.假 ...

  5. mysql 源代码编绎

    http://blog.chinaunix.net/uid-20723616-id-769326.html https://software.intel.com/zh-cn/blogs/2010/08 ...

  6. 如何使用==操作符,Equals方法,ReferenceEquals方法,IEquatable接口比较2个对象

    "世界上不会有两片完全相同的树叶",这句话适用于现实世界.而在软件世界中,这句话变成了"世界上必须有两片完全相同的树叶",否则,很多事情无以为继. 当比较2个对 ...

  7. 算法:优先级队列(PriorityQueue)

    背景 此文给出基于已排序数组的实现,多数情况应该基于 Heap 进行实现,因为数组的插入效率为O(n),而 Heap 的插入效率为 Log(n). PriorityQueue 代码 using Sys ...

  8. OpenStreetMap地图数据介绍(转)

    原文链接:每日一读 Packtpub.OpenStreetMap(1) 相信绝大多数人都知道Wiki,但要提起地图,大家第一反应肯定是Google地图.在没看这本书之前,还真不知原来还有OpenStr ...

  9. 如何使用Windows Library文件进行持久化

    前言 想象一下,假设在你不知道的情况下,攻击者在你的计算机上放置了一个恶意文件.每当你访问桌面上某个文件夹时(例如Documents文件夹),都会执行一次该文件.这样的场景,通过利用一种鲜为人知的持久 ...

  10. C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数

    在编程中经常需要用到数字与字符串的转换,下面就总结一下. 1.atoi() C/C++标准库函数,用于字符串到整数的转换. 函数原型:int atoi (const char * str); #inc ...