[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1905
  • Last Modified:

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
0
JoshdanG
Asked:
JoshdanG
  • 3
1 Solution
 
RichieHindleCommented:
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.
0
 
RichieHindleCommented:
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.
0
 
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.

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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now