How do I get monitor resolution in Python?

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

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

30条回答
  •  醉酒成梦
    2020-11-22 14:50

    A lot of these answers use tkinter to find the screen height/width (resolution), but sometimes it is necessary to know the dpi of your screen cross-platform compatible. This answer is from this link and left as a comment on another post, but it took hours of searching to find. I have not had any issues with it yet, but please let me know if it does not work on your system!

    import tkinter
    root = tkinter.Tk()
    dpi = root.winfo_fpixels('1i')
    

    The documentation for this says:

    winfo_fpixels(number)
    # Return the number of pixels for the given distance NUMBER (e.g. "3c") as float
    

    A distance number is a digit followed by a unit, so 3c means 3 centimeters, and the function gives the number of pixels on 3 centimeters of the screen (as found here). So to get dpi, we ask the function for the number of pixels in 1 inch of screen ("1i").

提交回复
热议问题