Detecting system DPI settings in Python

Posted on 2006-03-29
Last Modified: 2012-06-27
Is there any way to get the current DPI settings through Python?  

The particular scenario I am most concerned with is setting the appropriate width of a wx.DatePickerCtrl for varied settings of DPI in Windows NT/XP. Changing the "Font Size"/"DPI Setting" in (Display Properties > Settings > Advanced > General) causes fonts of any given point size to have a different width in pixels, so a date picker with fixed width in pixels is either too wide or to narrow depending upon this setting.

A cross-platform solution is preferred, and at the very least it should be able to fall back to a chosen default for platforms where it cannot determine the DPI.  Ideally the solution also wouldn't use anything unavailable with just Python 2.4.1, wxPython and a default installation of the operating system.

Question by:JoshdanG
    LVL 14

    Expert Comment

    I don't believe there's a way of doing this with stock Python, but it's easy with ctypes ( ) -

    from ctypes import *
    dc = windll.user32.GetDC(0)
    print "Horizontal DPI is", windll.gdi32.GetDeviceCaps(dc, LOGPIXELSX)
    print "Vertical DPI is", windll.gdi32.GetDeviceCaps(dc, LOGPIXELSY)
    windll.user32.ReleaseDC(0, dc)

    (It prints 96 and 96 on my machine.)

    I'm not sure what you mean by "cross-platform", given that you explicitly mention "Windows NT/XP".  This code will work on any Windows box, but not on any other OS.
    LVL 14

    Accepted Solution

    I'm wrong.  8-)  You *can* do it with only wxPython:

    from wx import App, ScreenDC
    app = App(0)
    s = ScreenDC()
    print s.GetPPI()

    As far as I'm aware, that really *is* cross-platform.
    LVL 2

    Author Comment

    Perfect.  Mind if I ask how you found that?

    The bit about Windows was if there was a limited solution like the first one, that would be a whole lot more useful to me than something that assumed since I was talking about Python I must be running on GNU/Linux. However your second solution is even yet more useful.

    LVL 14

    Expert Comment

    "Mind if I ask how you found that?"  Not at all - I knew the Windows way of doing it, hence my first answer.  Then I googled for GetDeviceCaps and wxWindows to see whether wxWindows had an equivalent, and that led me to GetPPI.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now