Detecting system DPI settings in Python

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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JoshdanGAuthor Commented:
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.

"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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.