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. SVN服务端的版本对比及创建仓库时的注意事项

    SVN是一个开放源代码的版本控制系统,分为客户端和服务端.就windows系统而言,客户端通常使用 TortoiseSVN,下载地址:https://tortoisesvn.net/  ,而服务端通常 ...

  2. HTTP请求头和响应头

      这篇文章简单总结一下HTTP请求头和响应头,并举一些web开发中响应头的用例. 1. HTTP请求头 accept:浏览器通过这个头告诉服务器,它所支持的数据类型.如:text/html, ima ...

  3. Android中播放本地SD卡中歌曲须要的加入的权限

    使用MediaPlayer播放本地Mp3文件时.须要注意的訪问路径的问题以及訪问权限的问题. 1.訪问路径:/storage/emulated/0 此路径即为手机的根路径,能够通过下载ES文件浏览器软 ...

  4. How do I use Tasker to run a sync in FolderSync?

    First of all the full version is required.     The full version works as a Tasker plugin - when you ...

  5. 【Go入门教程5】流程(if、goto、for、switch)和函数(多个返回值、变参、传值与传指针、defer、函数作为值/类型、Panic和Recover、main函数和init函数、import)

    这小节我们要介绍Go里面的流程控制以及函数操作. 流程控制 流程控制在编程语言中是最伟大的发明了,因为有了它,你可以通过很简单的流程描述来表达很复杂的逻辑.Go中流程控制分三大类:条件判断,循环控制和 ...

  6. MVC实现有关时间的进度条,使用jQuery ui的progressbar

    在电商网站中,有时候通过进度条来直观地显示用户是否到期以及用户当前的状态. 设计这样的一个Model. public class User { public int Id { get; set; } ...

  7. 关于WEB集群中文件服务器的讨论

    原文地址: http://blog.itpub.net/29806344/viewspace-1364778/ 在WEB集群中一般都要上传和删除图片.小规模的时候,图片放在本地,再通过同步方式来保持一 ...

  8. Js 日期加减天数

    <SCRIPT language="javascript"> function addDate(dd,dadd){ var a = new Date(dd) a = a ...

  9. python笔记26-命令行传参sys.argv实际运用

    前言 平常我们在用别人写好的python包的时候,在cmd输入xx -h就能查看到帮助信息,输入xx -p 8080就能把参数传入程序里,看起来非常酷. 本篇就来讲下如何在python代码里加入命令行 ...

  10. eclipse 设置打开java文件代码自动折叠

    eclipse 设置打开java文件代码自动折叠 java: windows/preference/java/editor/folding->enable folding 可以在里面设置所要折叠 ...