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.
Centering window on the screen的更多相关文章
- 《JAVASCRIPT高级程序设计》window/location/navigator/screen/history对象
如果要在web中使用JAVASCRIPT,那么BOM(浏览器对象模型)毫无疑问是最重要的部分.BOM提供了很多对象,例如,window.location.navigator.screen.histor ...
- JS BOM基础 全局对象 window location history screen navigator
全局变量声明的两种方式:1,window.变量名=值;2,var 变量名=值; 全局函数声明的两种方式:1,window.函数名=function(){}2,function 函数名=function ...
- 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 ...
- window对象的screen详解
screen.availHeight 返回屏幕的高度(不包括Windows任务栏)screen.availWidth 返回屏幕的宽度(不包括Windows任务栏)screen.colo ...
- JavaScript之BOM五大对象(window;location;navigator;screen;history)
一.window 1.浏览器窗口与页面各部分尺寸 2.间歇调用与超时调用 setInterval(function,time(以毫秒计)) 返回:时间Id号(为clearInterval(timeId ...
- [browser window窗口大小 算是screen补充吧]主要因为移动IE游览器 写了个兼容
先上图吧 来上代码 console.log(window.outerWidth + '--' + window.outerHeight);//只读的整数,声明了整个窗口的XY //IE 不支持此属性, ...
- [虾扯蛋] android界面框架-Window
从纯sdk及framwork的角度看,android中界面框架相关的类型有:Window,WindowManager,View等.下面就以这几个类为出发点来概览下安卓开发的"界面架构&quo ...
- JavaScript权威设计--Window对象(简要学习笔记十三)
1.Window对象是所有客户端JavaScript特性和API的主要接入点. Window对象中的一个重要属性是document,它引用Document对象. JavaScript程序可以通过Doc ...
- Navigator对象、Screen对象
Navigator对象: Window对象的navigator属性引用的是包含浏览器厂商和版本信息的Navigator对象: Navigator对象集合:plugins[] 返回对 ...
随机推荐
- Go语言Web框架gwk介绍 (四)
事件 gwk支持事件系统,但并没有硬编码有哪些事件,而是采用了比较松散的定义方式. 订阅事件有两种方式: 调用On函数或者OnFunc函数 func On(moudle, name string, h ...
- windows命令行快捷操作
net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user: ...
- 从零开始搭建linux下laravel 5.5所需环境(三)
好的,我们已经安装好了nginx+mysql+php了,打开[ Laravel 5.5 文档 ] 快速入门 —— 安装配置篇 我们看到这里需要安装Composer,好的,我们现在就来安装Compose ...
- HDU 1890 Robotic Sort (splay tree)
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Effective OC : 1-5
1,了解Objective-C语言的起源: OC为C语言的超集,为C加入了面向对象的特性. 要理解C中的指针和内存模型. 2.在类文件里尽量少引入其它头文件: 引入过多头文件.将借口暴露,添加耦合度. ...
- golang 实现轻量web框架
经常看到很多同学在打算使用go做开发的时候会问用什么http框架比较好.其实go的 http package 非常强大,对于一般的 http rest api 开发,完全可以不用框架就可以实现想要的功 ...
- MYSQL 源代码学习
http://ourmysql.com/ http://blog.chinaunix.net/uid-26896862-id-4009777.html
- Windows程序的打包,部署(vs项目打包vs2013)---ShinePans
Windows 应用程序在开发完毕之后,怎样将程序打包并制作成安装程序在客户机上部署 是每一个windows应用程序开发完毕之后都必须面对的问题. 学习目标: 部 ...
- Linux下open与fopen的区别
int open(const char *path, int access,int mode) path 要打开的文件路径和名称 access 访问模式,宏定义和含义如下: ...
- [android]ShareSDK——内容分享和短信验证
前言 新版本号ShareSDK的分享和短信验证,按官网的文档,都须要加入一个<Activity></Activity>标签,而分享和短息验证的这个标签内容都一样.会冲突. 解决 ...