Skip to content

Instantly share code, notes, and snippets.

@StanGenchev
Created December 22, 2019 12:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StanGenchev/266730fa16b4ffcd3f0799965bf880f0 to your computer and use it in GitHub Desktop.
Save StanGenchev/266730fa16b4ffcd3f0799965bf880f0 to your computer and use it in GitHub Desktop.
Example showing how to get monitor resolution and/or work area in Python 3 and Gtk 3.22+.
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="Gtk get screen workarea", application=app)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
print(self.get_workarea(win))
win.show_all()
def get_workarea(self, window):
# Returns Gdk.Screen
screen = window.get_screen()
# Returns Gdk.Display
display = screen.get_display()
# Gets the monitor on which the application is going to be shown on
# This prevents conflicts when the user has multiple monitors
monitor = display.get_monitor_at_window(screen.get_root_window())
# Requests the monitor workarea
# Return type is GdkRectangle (x, y, width, height)
# If you want the entire monitor resolution, replace with get_geometry()
workarea = monitor.get_workarea()
return workarea.width, workarea.height
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment