We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Detecting system DPI settings in Python

JoshdanG
JoshdanG asked
on
Medium Priority
3,861 Views
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 2.6.1.0 and a default installation of the operating system.

Thanks
Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.