Getting screen dims platform specific? Say it ain't so!

L

Lionel

Hello people, I'm looking for a way to get the screen dimensions (in
pixels) using the standard Python library. The only thing I found so
far was the following:

from win32api import GetSystemMetrics
Width = GetSystemMetrics(0)
Height = GetSystemMetrics(1)

I get an error claiming "no module named win32api". It's platform
specific anyway, but I thought I would try.

Anyone got this?

-L
 
L

Lionel

I'd guess that win32api is patform specific, as in "api for win32".

Yes, it's platform specific. I was just trying anyway to see if it
would work.

In a nutshell, what I need is a way to acquire the screen dimensions
(in pixels) and its dpi setting.
 
J

John McMonagle

Lionel said:
Yes, it's platform specific. I was just trying anyway to see if it
would work.

In a nutshell, what I need is a way to acquire the screen dimensions
(in pixels) and its dpi setting.

This should work on all platforms:

from Tkinter import *
r = Tk()
r.withdraw()
r.winfo_screenheight()
r.winfo_screenwidth()
r.winfo_pixels('1i')


Regards,

John
 
L

Luis Zarrabeitia

In a nutshell, what I need is a way to acquire the screen dimensions
(in pixels) and its dpi setting.

This is a guess, and a guess only.
What do you want to know it for? If it is for painting something on the
screen, maybe (just maybe - I'm guessing here big time), whatever API you are
using for painting can be used for getting the dimensions of the drawing
device. I don't even know if GTK or Qt have that feature, but you should look
into it. If the same api you will use for painting provides that
functionality, then it would be kind of irrelevant if it is on the stdlib or
not.

Of course, this only applies if you want to paint and your api provides that
info.
 
L

Lionel

This is a guess, and a guess only.
What do you want to know it for? If it is for painting something on the
screen, maybe (just maybe - I'm guessing here big time), whatever API you are
using for painting can be used for getting the dimensions of the drawing
device. I don't even know if GTK or Qt have that feature, but you should look
into it. If the same api you will use for painting provides that
functionality, then it would be kind of irrelevant if it is on the stdlib or
not.

Of course, this only applies if you want to paint and your api provides that
info.

Thanks for the responses so far, everyone. Luis you are indeed a good
guesser! I'm using wxPython/matplotlib and would like to size my
matplotlib data figure so that it assumes a 1:1 ratio on the screen
when embedded in a wxPython form. I'm playing with a 2D array with 320
rows and 177 columns and therefore would like to display the figure
with a size of 320x177 pixels. The problem is that the call to
instantiate the figure takes size parameters of inches and dpi! For
example:

matplotlib.figure.Figure((5,4), 75)

is a figure with width of 5 inches, height of 4 inches, and dpi of 75.
Thanks to Luis' comment I googled around and found that wxPython has
the following functions:

wx.GetDisplaySize() # Get dimensions in pixels.
wx.GetDisplaySizeMM() # Get dimensions in millimeters.

It's a little klunky to have a function that takes inches an an
argument and another that produces millimeters when querying the
display, no? Unless someone has a better idea I suppose I'll have to
convert between the two to display at 1:1.

-L

SInce I want my matplotlib.figure.Figure() to assume the exact pixel
dimensions of the 2D array,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top