Ingo Karkat
Ingo Karkat

Reputation: 172520

How to get total screen size in Python / GTK without using deprecated Gdk.Screen.get_width()?

I want the X/Y pixel dimensions of the entire desktop (potentially spanning multiple monitors), e.g. to determine the optimal resolution of a background image.

This Python code still works:

from gi import require_version
require_version("Gdk", "3.0")
from gi.repository import Gdk
screen = Gdk.Screen.get_default()
print(screen.get_width(), " ", screen.get_height())

but prints a deprecation warning:

<string>:7: DeprecationWarning: Gdk.Screen.get_width is deprecated
<string>:7: DeprecationWarning: Gdk.Screen.get_height is deprecated

The Gdk.Screen API docs just note:

Deprecated since version 3.22: Use per-monitor information instead

Other answers (like How do I get monitor resolution in Python or How to detect a computer's physical screen size in GTK) mention lots of other APIs and toolkits, or just give per-monitor information (like this for PyGtk), but I think it should still be possible (also in the future) to get the dimensions of the entire desktop via PyGtk. (After all, the deprecated function also still provides this.)

Upvotes: 4

Views: 3072

Answers (3)

dumbass
dumbass

Reputation: 27215

Based on the commit which removed those API calls, the algorithm that GDK uses to compute the screen size seems to be basically this:

def get_screen_size(display):
    mon_geoms = [
        display.get_monitor(i).get_geometry()
        for i in range(display.get_n_monitors())
    ]

    x0 = min(r.x            for r in mon_geoms)
    y0 = min(r.y            for r in mon_geoms)
    x1 = max(r.x + r.width  for r in mon_geoms)
    y1 = max(r.y + r.height for r in mon_geoms)

    return x1 - x0, y1 - y0

# example use
print(get_screen_size(Gdk.Display.get_default()))

Upvotes: 7

Tch
Tch

Reputation: 1055

I think this is the simplest to be honest

from gi import require_version
require_version("Gdk", "3.0")
from gi.repository import Gdk

screen = Gdk.Display.get_default()
w=0
h=0
for x in range(0, screen.get_n_monitors()):
     w += screen.get_monitor(x).get_geometry().width
     if ( h < screen.get_monitor(x).get_geometry().height ):
          h = screen.get_monitor(x).get_geometry().height
print (w, ' ', h)

Upvotes: 0

kasztp
kasztp

Reputation: 11

I have no GTK installed so unfortunately can't test, but I think something like this should work:

from gi.repository import Gdk

display = Gdk.Display.get_default()
monitor = display.get_primary_monitor()
scale_factor = monitor.get_scale_factor()
geometry = monitor.get_geometry()
x = geometry.x * scale_factor
y = geometry.y * scale_factor

print(f'Screen Size: {x}x{y}')

Based on this: https://developer.gnome.org/gdk3/stable/GdkMonitor.html#gdk-monitor-get-scale-factor

This: https://github.com/AdoptOpenJDK/openjdk-support/issues/172#issuecomment-497111004

And this: https://stackoverflow.com/a/63535459/7200940

Upvotes: 0

Related Questions