How can I get the screen size in Tkinter?

Question:

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…

Asked By: DonJuma

||

Answers:

import tkinter as tk

root = tk.Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
Answered By: mouad

A possible solution

import os

os.system("xrandr  | grep * | cut -d' ' -f4")

My output:

1440x900
0
Answered By: Nandit Tiku

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
Answered By: VoteCoffee
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.