当前位置:   article > 正文

使用python获取电脑主机和显示器的硬件详情_python获取本机电脑型号

python获取本机电脑型号
  1. import wmi
  2. import winreg
  3. import os
  4. import math
  5. #import tkinter as tk
  6. from tkinter import *
  7. import win32api,win32con
  8. # 获取显示器 主机配置信息
  9. w = wmi.WMI()
  10. global list
  11. list = []
  12. def info():
  13. # 显示器配置信息
  14. PATH = "SYSTEM\\ControlSet001\\Enum\\"
  15. # 获取屏幕信息
  16. monitors = w.Win32_DesktopMonitor()
  17. list.append("-------------------显示器配置信息--------------------")
  18. for m in monitors:
  19. subPath = m.PNPDeviceID #
  20. # 可能有多个注册表
  21. if subPath == None:
  22. continue
  23. # 这个路径这里就是你的显示器在注册表中的路径,比如我现在的电脑是在HKEY_LOCAL_MACHINE下面的路径:
  24. # \SYSTEM\ControlSet001\Enum\DISPLAY\CMN1604\1&8713bca&0&UID0\Device Parameters
  25. infoPath = PATH + subPath + "\\Device Parameters"
  26. key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, infoPath)
  27. # 屏幕信息按照一定的规则保存(EDID)
  28. value = winreg.QueryValueEx(key, "EDID")[0]
  29. winreg.CloseKey(key)
  30. # 屏幕实际尺寸
  31. width, height = value[21], value[22]
  32. # 推荐屏幕分辨率
  33. widthResolution = value[56] + (value[58] >> 4) * 256
  34. heightResolution = value[59] + (value[61] >> 4) * 256
  35. # 屏幕像素密度(Pixels Per Inch)
  36. widthDensity = widthResolution / (width / 2.54)
  37. heightDensity = heightResolution / (height / 2.54)
  38. # print("屏幕宽度:", str(width), " (厘米)")
  39. # print("屏幕高度:", str(height), " (厘米)")
  40. # print("水平分辩率: ", str(widthResolution), " (像素)")
  41. # print("垂直分辩率: ", str(heightResolution), " (像素)")
  42. # # 保留小数点固定位数的两种方法
  43. # print("水平像素密度: ", round(widthDensity, 2), " (PPI)")
  44. # print("垂直像素密度: ", "%2.f" % heightDensity, " (PPI)")
  45. width1 = "屏幕宽度" + str(width) + "(厘米)"
  46. height1 = "屏幕高度:" + str(height) + " (厘米)"
  47. yingcun = math.sqrt(int(width * width + height * height))
  48. yc = format(yingcun / 2.54, '.0f')
  49. yingcun1 = "屏幕尺寸:" + str(yc) + "(英寸)"
  50. widthResolution1 = "水平分辩率: " + str(widthResolution) + " (像素)"
  51. heightResolution1 = "垂直分辩率: " + str(heightResolution) + " (像素)"
  52. # data =[]
  53. list.append(width1)
  54. list.append(height1)
  55. list.append(yingcun1)
  56. list.append(widthResolution1)
  57. list.append(heightResolution1)
  58. list.append("------------------电脑信息--------------------")
  59. for BIOSs in w.Win32_ComputerSystem():
  60. list.append("电脑名称: %s" %BIOSs.Caption)
  61. list.append("使 用 者: %s" %BIOSs.UserName)
  62. #获取序列号
  63. for zb_msg in w.Win32_BaseBoard():
  64. zbs=str(zb_msg.SerialNumber)
  65. try:
  66. xlh=zbs.split('/')[1]
  67. list.append("序列号: %s" % str(xlh))
  68. except IndexError as e:
  69. print(e)
  70. try:
  71. xlh = zbs.split('.')[1]
  72. list.append("序列号: %s" % str(xlh))
  73. except IndexError as e:
  74. list.append("序列号1: %s" % str(zbs))
  75. #zbs = str(zb_msg.SerialNumber).split('/')[1]
  76. #list.append("序列号: %s" % str(zb_msg.SerialNumber))
  77. #print(str(zb_msg.SerialNumber).split('/'))
  78. #list.append("序列号: %s" % zbs)
  79. for address in w.Win32_NetworkAdapterConfiguration(ServiceName = "e1dexpress"):
  80. list.append("IP地址: %s" % address.IPAddress[0])
  81. list.append("MAC地址: %s" % address.MACAddress)
  82. for BIOS in w.Win32_BIOS():
  83. list.append("使用日期: %s" %BIOS.Description)
  84. list.append("主板型号: %s" %BIOS.SerialNumber)
  85. for processor in w.Win32_Processor():
  86. list.append("CPU型号: %s" % processor.Name.strip())
  87. for memModule in w.Win32_PhysicalMemory():
  88. totalMemSize=int(memModule.Capacity)
  89. list.append("内存厂商: %s" %memModule.Manufacturer)
  90. list.append("内存型号: %s" %memModule.PartNumber)
  91. list.append("内存大小: %.2fGB" %(totalMemSize/1024**3))
  92. for disk in w.Win32_DiskDrive(InterfaceType = "IDE"):
  93. diskSize=int(disk.size)
  94. list.append("磁盘名称: %s" %disk.Caption)
  95. list.append("磁盘大小: %.2fGB" %(diskSize/1024**3))
  96. for xk in w.Win32_VideoController():
  97. list.append("显卡名称: %s" %xk.name)
  98. def main():
  99. info()
  100. print(len(list))
  101. # 创建主窗口
  102. win = Tk()
  103. win.title('电脑配置')
  104. text = Text(win, width=60, height=30, undo=True, autoseparators=False)
  105. text.pack()
  106. for i in list:
  107. text.insert(INSERT, str(i)+'\n')
  108. win.mainloop()
  109. with open('diannao.txt','w+',encoding='utf-8') as d:
  110. for li in list:
  111. print(li)
  112. l=li+"\n"
  113. d.write(l)
  114. main()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/156798?site
推荐阅读
相关标签
  

闽ICP备14008679号