-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ScreenNotify.py
64 lines (55 loc) · 2.42 KB
/
ScreenNotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2021/4/13
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@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_())