第一種:通過調用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_())