第一种:通过调用Windows API来获得当前屏幕分辨率。
需要安装pywin32模块库,然后程序导入:
from win32 import win32api, win32gui, win32print from win32.lib import win32con """获取缩放后的分辨率""" sX = win32api.GetSystemMetrics(0) #获得屏幕分辨率X轴 sY = win32api.GetSystemMetrics(1) #获得屏幕分辨率Y轴 print(sX) print(sY) """获取真实的分辨率""" hDC = win32gui.GetDC(0) w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES) # 横向分辨率 h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES) # 纵向分辨率 print(w) print(h) # 缩放比率 screen_scale_rate = round(w / sX, 2) print(screen_scale_rate)
第二种:通过GUI模块的提供的函数获取
安装:pip install PyQt5
import sys
from PyQt5.QtWidgets import QApplication, QWidget
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI() # 界面绘制交给InitUi方法
def initUI(self):
self.desktop = QApplication.desktop()
# 获取显示器分辨率大小
self.screenRect = self.desktop.screenGeometry()
self.height = self.screenRect.height()
self.width = self.screenRect.width()
print(self.height)
print(self.width)
# 显示窗口
self.show()
if __name__ == '__main__':
# 创建应用程序和对象
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())