关于python:Python设置显示屏分辨率

12次阅读

共计 1800 个字符,预计需要花费 5 分钟才能阅读完成。

工具装置

  • 次要调用 win32 库实现分辨率获取和读写,须要装置 pywin32
  • 示例中是从 execl 列表中读取须要设置的分辨率,须要装置 xlrd
  • 用到的 execl 分辨率列表如下:
pip install pywin32
pip install xlrd

残缺脚本

import win32api,win32con,time,xlrd

"""
Author:NoamaNelson
Date:2019-11-26
Discription:Python sets the resolution of Windows computer graphics card and obtains the resolution
"""book1 = xlrd.open_workbook(' 常见分辨率.xlsx') # 关上表格
q = book1.sheet_by_index(0) # 应用索引的形式获取 sheet1 工作簿
i = 0
while(i<=5):
    print("第 %d 次设置"%i)
    mwidth = str(q.cell_value(i+1,0))    # 获取单元格中的值
    nwidth = mwidth.split(".")[0]    # 获取的值为 float,转换成字符串而后进行宰割取整
    WidthSet = int(nwidth) # 从表格中获取的分辨率宽的值
    #print(WidthSet)

    mhigth = str(q.cell_value(i+1,1))    # 获取单元格中的值
    nhigth = mhigth.split(".")[0]    # 获取的值为 float,转换成字符串而后进行宰割取整
    HeightSet = int(nhigth)    # 从表格中获取的分辨率宽的值
    #print(HeightSet)
    
    ResoLutionSet = win32api.EnumDisplaySettings(None, 0) # 调用 win32api 接口, 获取显示设施信息

    ResoLutionSet.PelsHeight = HeightSet # 设置分辨率高
    ResoLutionSet.PelsWidth = WidthSet # 设置分辨率宽
    print("设置的分辨率宽: %d, 设置的分辨率高: %d, 即设置的分辨率为:%d x %d" % (WidthSet, HeightSet, WidthSet, HeightSet))

    ResoLutionSet.BitsPerPel = 32 # 显示设施的色彩分辨率
    ResoLutionSet.DisplayFixedOutput = 2 # 设置分辨率后拉伸画面,否则切换到小分辨率时,屏幕只在两头一小块

    if (win32api.ChangeDisplaySettings(ResoLutionSet, 0)) == 0: # DISP_CHANGE_SUCCESSFUL
        win32api.ChangeDisplaySettings(ResoLutionSet, 0) # 设置失效
    else:
        win32api.ChangeDisplaySettings(None, 0) # 复原默认

    screenNum = win32api.GetSystemMetrics(win32con.SM_CMONITORS)
    print("显示设施的总数量为: %d" % screenNum)

    aScreenWidth = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
    aScreenHeight = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
    print("以后屏幕总的分辨率为:%d × %d" % (aScreenWidth, aScreenHeight))

    WidthGet = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) # 取得屏幕分辨率 X 轴
    HeightGet = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) # 取得屏幕分辨率 Y 轴
    print ("获取的分辨率宽: %d, 获取的分辨率高: %d, 即获取的分辨率为:%d x %d" % (WidthGet, HeightGet, WidthGet, HeightGet))
    time.sleep(3)
   
    i += 1

输入的后果

存在的问题

目前应用这种形式,只能设置显卡列表反对的分辨率,不反对自定义分辨率。不晓得有什么更好的形式,有晓得的大牛给领导下。

参考资料

微软官网材料
其余博客阐明

正文完
 0