109 Star 861 Fork 308

PyQt5 / PyQt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ScreenNotify.py 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
Irony 提交于 2021-07-13 14:52 . support PySide2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2021/4/13
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: 892768447@qq.com
@file: ScreenNotify
@description: 屏幕、分辨率、DPI变化通知
"""
import sys
try:
from PyQt5.QtCore import QTimer, QRect
from PyQt5.QtWidgets import QApplication, QPlainTextEdit
except ImportError:
from PySide2.QtCore import QTimer, QRect
from PySide2.QtWidgets import QApplication, QPlainTextEdit
class Window(QPlainTextEdit):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.appendPlainText('修改分辨率后查看')
# 记录最后一次的值(减少槽调用)
self.m_rect = QRect()
# 使用定时器来延迟触发最后一次变化
self.m_timer = QTimer(self, timeout=self.onSolutionChanged)
self.m_timer.setSingleShot(True) # **重要** 保证多次信号尽量少的调用函数
# 主要是多屏幕->无屏幕->有屏幕
QApplication.instance().primaryScreenChanged.connect(lambda _: self.m_timer.start(1000))
# 其它信号最终基本上都会调用该信号
QApplication.instance().primaryScreen().virtualGeometryChanged.connect(
lambda _: self.m_timer.start(1000))
# DPI变化
QApplication.instance().primaryScreen().logicalDotsPerInchChanged.connect(
lambda _: self.m_timer.start(1000))
def onSolutionChanged(self):
# 获取主屏幕
screen = QApplication.instance().primaryScreen()
if self.m_rect == screen.availableVirtualGeometry():
return
self.m_rect = screen.availableVirtualGeometry()
# 所有屏幕可用大小
self.appendPlainText('\navailableVirtualGeometry: {0}'.format(str(screen.availableVirtualGeometry())))
# 获取所有屏幕
screens = QApplication.instance().screens()
for screen in screens:
self.appendPlainText(
'screen: {0}, geometry({1}), availableGeometry({2}), logicalDotsPerInch({3}), '
'physicalDotsPerInch({4}), refreshRate({5})'.format(
screen.name(), screen.geometry(), screen.availableGeometry(), screen.logicalDotsPerInch(),
screen.physicalDotsPerInch(), screen.refreshRate()))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
Python
1
https://gitee.com/PyQt5/PyQt.git
git@gitee.com:PyQt5/PyQt.git
PyQt5
PyQt
PyQt
master

搜索帮助