Screen Size

or a great chance to use python's new format() function


ScreenSize.py


import ctypes as c

user32 = c.windll.user32
SM_CXSCREEN = 0
SM_CYSCREEN = 1

MB_OK = 0x0

def MessageBoxFormat(caption, format, *params):
    return user32.MessageBoxW(0, format.format(*params), caption, MB_OK)

if __name__ == "__main__":
    x = user32.GetSystemMetrics(SM_CXSCREEN)
    y = user32.GetSystemMetrics(SM_CYSCREEN)

    MessageBoxFormat("Screen Size", 
                     "The Screen is {0} pixels wide and {1} pixels high.", 
                     x, y)

Python 3 really makes this a simple program. Petzold's aim with the original was to create a version of MessageBox that could accept additional parameters and a format string just like printf. Using Python 2.x we could have achieved the same thing using the % operator and the variable parameter list (*params in our example here.) But instead of the old school formatting I choose python 3 formatting.

Python 2.x would have used:


>>>"He baked %s till %d o'clock." % ('cookies', 3)
He baked cookies till 3 o'clock.

in Python 3 we now have a new format option:


>>>"She baked {0} till {1} o'clock.".format('brownies', 3)
She baked cookies till 12 o'clock.

The clear advantage of the new format function is that you don't have to commit to a datatype when you write your code.

changed January 3, 2009