Link to home
Start Free TrialLog in
Avatar of JoshdanG
JoshdanG

asked on

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 2.6.1.0 and a default installation of the operating system.

Thanks
Avatar of RichieHindle
RichieHindle

I don't believe there's a way of doing this with stock Python, but it's easy with ctypes ( http://starship.python.net/crew/theller/ctypes/ ) -

from ctypes import *
LOGPIXELSX = 88
LOGPIXELSY = 90
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.
ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JoshdanG

ASKER

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.

Thanks!
"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.