How can I get the screen size in Tkinter?

Viewed 68056

I would like to know if it is possible to calculate the screen size using Tkinter.

I wanted this so that can make the program open up in the center of the screen...

3 Answers

For Windows:

You can make the process aware of DPI to handle scaled displays.

import ctypes
try: # Windows 8.1 and later
    ctypes.windll.shcore.SetProcessDpiAwareness(2)
except Exception as e:
    pass
try: # Before Windows 8.1
    ctypes.windll.user32.SetProcessDPIAware()
except: # Windows 8 or before
    pass

Expanding on mouad's answer, this function is capable of handling multi-displays and returns the resolution of the current screen:

import tkinter
def get_display_size():
    root = tkinter.Tk()
    root.update_idletasks()
    root.attributes('-fullscreen', True)
    root.state('iconic')
    height = root.winfo_screenheight()
    width = root.winfo_screenwidth()
    root.destroy()
    return height, width
Related