How to detect a computer's physical screen size in GTK
Solution 1
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...
Solution 2
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.
Solution 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.
Solution 4
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()
Related videos on Youtube
David Planella
I work at GitLab as Director of Community Relations. Before, I worked for Canonical as the former Ubuntu Community Team Manager. As an Open Source contributor, I am mostly involved in app development and localization: I'm the developer of Qreator, former lead the Ubuntu Catalan Translators team and also a GNOME translator. In the past I've contributed to other projects, such as Debian or Mozilla. Blog Google+ Twitter
Updated on September 18, 2022Comments
-
David Planella over 1 year
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?
-
Vilmantas Baranauskas almost 12 yearsJust brainstorming: What about multi-monitor setups with different sizes?
-
dobey almost 12 yearsIt seems like using the screen size here is the wrong solution. Are there good screenshots of the windows where this is a problem?
-
David Planella almost 12 yearsThere are a few in the bug reports: bugs.launchpad.net/qreator/+bug/1015138/+attachment/3196020/… and launchpadlibrarian.net/107989779/…
-
-
dobey almost 12 yearsYou actually need to poke at the Display rather than the Screen, for getting individual display sizes, IIRC.
-
web.learner over 9 yearsTested on 14.04, it works.
-
guettli almost 9 yearsThank you very much. Your solution works even if the window is not realized yet.
-
guettli almost 9 yearsIf 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.
-
NoBugs over 4 yearsI see DeprecationWarning: get_width is deprecated
-
matanster almost 4 yearsWorks well for a dual display setup
-
matanster almost 4 yearsAnd for multiple monitors use
Gdk.Display().display.get_n_monitors()
to get all applicable indices for.get_monitor
.