PyQt5每天必学之创建窗口居中效果
作者:我的世界你曾经来过 时间:2022-02-16 19:03:49
本文实例为大家分享了PyQt5如何能够创建在桌面屏幕上居中窗口的具体代码,供大家参考,具体内容如下
下面的脚本说明我们如何能够创建在桌面屏幕上居中的窗口。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
PyQt5 教程
这个程序是将一个窗口显示在屏幕的中心。
作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年7月30日
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.center()
self.setWindowTitle('窗口居中')
self.show()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
QtGui.QDesktopWidget 类提供了有关用户桌面的信息,包括屏幕尺寸。
self.center()
将居中窗口的代码放置在自定义 center() 方法中。
qr = self.frameGeometry()
frameGeometry() 方法允许我们创建一个无形矩形并根据主窗口的宽高设置自身的宽度与高度。简单理解就是将这个控件(QWidget)的几何内容(宽高位置等),赋值给qr
cp = QDesktopWidget().availableGeometry().center()
计算出你的显示器的屏幕分辨率。根据得到的分辨率我们得到屏幕的中心点。
qr.moveCenter(cp)
我们的矩形(qr)已有宽度和高度,现在设置移动矩形的中心(moveCenter)到屏幕的中心点(cp),矩形的尺寸是不变的。
self.move(qr.topLeft())
移动应用程序窗口的左上角到qr矩形的左上角,从而使应用程序窗口显示在屏幕的中心。
程序执行后
来源:https://blog.csdn.net/weiaitaowang/article/details/52072992
标签:PyQt5,窗口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
TensorFlow安装及jupyter notebook配置方法
2022-09-11 01:05:23
python日记(使用TCP实现的对话客户端和服务器)
2023-01-25 19:42:54
python爬虫框架Scrapy基本应用学习教程
2021-04-05 13:09:44
![](https://img.aspxhome.com/file/2023/8/94108_0s.png)
解决Python selenium get页面很慢时的问题
2022-08-29 02:08:22
![](https://img.aspxhome.com/file/2023/6/90596_0s.jpg)
Python Cookie 读取和保存方法
2021-01-21 15:57:51
详解MySQL数据库安全配置
2010-01-26 15:19:00
深入理解Django的中间件middleware
2023-05-11 18:29:46
![](https://img.aspxhome.com/file/2023/9/123519_0s.jpg)
原生js实现瀑布流效果
2023-09-04 07:11:02
Python学习之asyncore模块用法实例教程
2023-07-30 14:16:46
Python可变集合和不可变集合的构造方法大全
2021-10-23 12:39:04
JavaScript的目的及历史
2007-10-17 18:53:00
python七种方法判断字符串是否包含子串
2023-09-19 04:06:20
Pycharm配置autopep8实现流程解析
2021-08-23 05:34:40
![](https://img.aspxhome.com/file/2023/0/98850_0s.png)
python处理json文件的四个常用函数
2023-01-17 21:26:58
![](https://img.aspxhome.com/file/2023/5/128345_0s.png)
如何利用Python解析超大的json数据(GB级别)
2023-03-22 12:08:21
![](https://img.aspxhome.com/file/2023/9/135069_0s.jpg)
asp中去除html中style,javascript,css代码
2011-02-16 11:18:00
pandas中的DataFrame数据遍历解读
2023-12-03 21:57:34
![](https://img.aspxhome.com/file/2023/7/93457_0s.png)
python实现m3u8格式转换为mp4视频格式
2021-05-09 00:48:02
![](https://img.aspxhome.com/file/2023/4/101174_0s.png)
Python设计模式之建造者模式实例详解
2021-07-13 17:37:03
![](https://img.aspxhome.com/file/2023/6/75866_0s.png)
对python中return与yield的区别详解
2022-10-19 00:47:33