42

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...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DonJuma
  • 2,028
  • 13
  • 42
  • 70
  • It depends on the platform you want to acomplish this. So maybe you should let us know on what operating systems this should work... – Titusz Oct 16 '10 at 17:24
  • 1
    you should find an answer in this stackoverflow question: http://stackoverflow.com/questions/3129322/how-do-i-get-monitor-resolution-in-python – Titusz Oct 18 '10 at 11:42

3 Answers3

103
import tkinter as tk

root = tk.Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
nbro
  • 15,395
  • 32
  • 113
  • 196
mouad
  • 67,571
  • 18
  • 114
  • 106
  • 9
    This method will fail with multiple screen setups, but there is a [workaround](https://stackoverflow.com/a/56913005/5218354) to that. – norok2 Jul 06 '19 at 10:02
  • 7
    This actually doesn't handle my current Windows config, running a 50" 4k TV. The above tells me that my monitor is 2560x1440 instead of the actual 3840x2160 with 150% scaling. So, the Python calls are coming back with the "scaled" resolution. – GaryMBloom Mar 03 '20 at 20:16
  • @Gary02127 same. The answer below actually helped me out. – 10 Rep Dec 28 '20 at 02:48
  • @10 Rep: Which answer? [VoteCoffee's answer](https://stackoverflow.com/questions/3949844/how-to-get-the-screen-size-in-tkinter/66248631#66248631)? – Peter Mortensen Oct 08 '21 at 09:20
  • @PeterMortensen This was a year ago so I don't remember but I believe it is Nandit TIku;s answer. – 10 Rep Oct 08 '21 at 15:56
6

A possible solution

import os

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

My output:

1440x900
0
Nandit Tiku
  • 583
  • 3
  • 5
  • 3
    Does this solution depend on X11? How likely is a user to have X11 on a mac? – Chris Lucian Jan 15 '16 at 05:07
  • 1
    @wookie: No obfuscated code, nor regex, just three commands available in any standard GNU/Linux system supporting X and RandR. Simply concatenated one after another by the pipe symbol. – Alberto Vassena Apr 06 '17 at 18:56
  • @Nandit Tiku: Nice and short code. Is it possible to somehow (I am a dummy linux-wise) give the found monitor sizes not as strings but as values to an output tuple or something? – Joris Apr 25 '17 at 08:51
  • This outputs the value to the console, but does not pass it to python. Working on stdout piped version... – RufusVS Sep 23 '17 at 17:02
  • A piped version (and other) in a comprehensive list here: https://stackoverflow.com/questions/3129322/how-do-i-get-monitor-resolution-in-python – RufusVS Sep 23 '17 at 17:20
  • 1
    Where does this work? What is xrandr? (["xrandr is an official configuration utility to the RandR (Resize and Rotate) X Window System extension"](https://wiki.archlinux.org/title/xrandr)). [RandR](https://en.wikipedia.org/wiki/X.Org_Server#Software_architecture) - *"The X.Org Server implements the server side of the X Window System core protocol version 11 (X11) and extensions to it, e.g. RandR."*. – Peter Mortensen Oct 08 '21 at 09:27
  • Can you elaborate? E.g. for the previous comment and on which system this was tested (Linux?), incl. version. Please respond by [editing (changing) your question/answer](https://stackoverflow.com/posts/3950195/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Oct 08 '21 at 09:28
1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VoteCoffee
  • 4,692
  • 1
  • 41
  • 44