11

I've come across this bug while writing my app. In short, the app's window size is fixed, which does not work well with smaller screens such as in netbooks.

In those, the lower buttons are off the screen limits and cannot be used. I'd like to take this into account in the user interface, but first of all, I'd like to find out what the standard way to detect screen size is in GTK, if there is one.

So does anyone have any ideas on how to do that?

3

4 Answers 4

14
from gi.repository import Gdk
s = Gdk.Screen.get_default()
print(s.get_width())
print(s.get_height())

Of course, if you have multiple screens, this gives the size of a rectangle enclosing them both. This stuff is harder than it sounds, in a world with multiple screens in it...

5
  • You actually need to poke at the Display rather than the Screen, for getting individual display sizes, IIRC.
    – dobey
    Jun 20, 2012 at 13:24
  • Tested on 14.04, it works.
    – Seth
    Sep 25, 2014 at 4:12
  • If you have more than one monitor the screen size is bigger. The above solution does not work if you want the size of a monitor. See the answer of @justin for that.
    – guettli
    Jul 9, 2015 at 19:00
  • I see DeprecationWarning: get_width is deprecated
    – NoBugs
    Jan 2, 2020 at 4:14
  • Works well for a dual display setup
    – matanox
    Jun 15, 2020 at 21:51
9

Here is what I came up with:

from gi.repository import Gdk, Gtk

# Replace w with the GtkWindow of your application
w = Gtk.Window()
# Get the screen from the GtkWindow
s = w.get_screen()
# Using the screen of the Window, the monitor it's on can be identified
m = s.get_monitor_at_window(s.get_active_window())
# Then get the geometry of that monitor
monitor = s.get_monitor_geometry(m)
# This is an example output
print("Heigh: %s, Width: %s" % (monitor.height, monitor.width))

I'm not sure that would be called 'standard,' but I hope that it helps.

1
  • Thank you very much. Your solution works even if the window is not realized yet.
    – guettli
    Jul 9, 2015 at 18:52
3

I would say what you want is the answer to the following question:

How big would a window be if it was maximized?

Because beside the multiple screen setup there are also other problems like the size of the top panel, bottom panels (no longer in Ubuntu, but maybe other distros), window decorators,... This is actually not handled by Gtk but by the window manager. So I don't see any way to get this number unless you let the windowmanager actually do the maximization.

Therefore the answer to this question is easy:

from gi.repository import Gtk, GObject
w = Gtk.Window()
w.maximize()
def p():
    print w.get_size()
GObject.timeout_add(1000, p)
w.show()
Gtk.main()

So you could create a window, maximize it and then start to populate it with reasonable sized widgets.

3

You can get width and height in millimeters with this method:

from gi.repository import Gdk
display = Gdk.Display.get_default()
monitor = display.get_monitor(0)
return monitor.get_width_mm(), monitor.get_height_mm()
1
  • And for multiple monitors use Gdk.Display().display.get_n_monitors() to get all applicable indices for .get_monitor.
    – matanox
    Jun 15, 2020 at 22:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .