PyQt/Demo/ScreenNotify.py

59 lines
2.2 KiB
Python
Raw Normal View History

2021-04-13 14:40:32 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2021/4/13
@author: Irony
@site: https://github.com/PyQt5
@email: 892768447@qq.com
@file: ScreenNotify
@description: 屏幕分辨率DPI变化通知
"""
import sys
from PyQt5.QtCore import QTimer, QRect
from PyQt5.QtWidgets import QApplication, QPlainTextEdit, qApp
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) # **重要** 保证多次信号尽量少的调用函数
# 主要是多屏幕->无屏幕->有屏幕
qApp.primaryScreenChanged.connect(lambda _: self.m_timer.start(1000))
# 其它信号最终基本上都会调用该信号
qApp.primaryScreen().virtualGeometryChanged.connect(lambda _: self.m_timer.start(1000))
# DPI变化
qApp.primaryScreen().logicalDotsPerInchChanged.connect(lambda _: self.m_timer.start(1000))
def onSolutionChanged(self):
# 获取主屏幕
screen = qApp.primaryScreen()
if self.m_rect == screen.availableVirtualGeometry():
return
self.m_rect = screen.availableVirtualGeometry()
# 所有屏幕可用大小
self.appendPlainText('\navailableVirtualGeometry: {0}'.format(str(screen.availableVirtualGeometry())))
# 获取所有屏幕
screens = qApp.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_())