How do I get monitor resolution in Python?

后端 未结 30 1454
深忆病人
深忆病人 2020-11-22 13:49

What is the simplest way to get monitor resolution (preferably in a tuple)?

30条回答
  •  情深已故
    2020-11-22 14:47

    On Linux we can use subprocess module

    import subprocess
    cmd = ['xrandr']
    cmd2 = ['grep', '*']
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    p2 = subprocess.Popen(cmd2, stdin=p.stdout, stdout=subprocess.PIPE)
    p.stdout.close()
    
    resolution_string, junk = p2.communicate()
    resolution = resolution_string.split()[0]
    resolution = resolution.decode("utf-8") 
    width = int(resolution.split("x")[0].strip())
    heigth = int(resolution.split("x")[1].strip())
    

提交回复
热议问题