Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Just brainstorming: What about multi-monitor setups with different sizes? – Vilmantas Baranauskas Jun 20 '12 at 12:16
It seems like using the screen size here is the wrong solution. Are there good screenshots of the windows where this is a problem? – dobey Jun 20 '12 at 13:30
from gi.repository import Gdk
s = Gdk.Screen.get_default()
print s.get_width(), 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...

share|improve this answer
You actually need to poke at the Display rather than the Screen, for getting individual display sizes, IIRC. – dobey Jun 20 '12 at 13:24
Tested on 14.04, it works. – Seth Sep 25 '14 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 '15 at 19:00

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.

share|improve this answer
Thank you very much. Your solution works even if the window is not realized yet. – guettli Jul 9 '15 at 18:52

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()
def p():
    print w.get_size()
GObject.timeout_add(1000, p)

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.