Search code examples
pythonlinuxgtk3pygobject

How to check the current screen dimensions in python / Gtk 3.0 without deprecation warnings?


I need to know dimensions (exactly: height) of the screen below the current Gtk.Window.The most frequently recommended method:

window = Gtk.Window()
screen = window.get_screen()
h = screen.height()

does the job, but gives me DeprecationWarning: Gdk.Screen.height is deprecated, and is likely to stop working sooner or later. I wouldn't like to add any new dependencies, so this cool cheat sheet doesn't help.

My code is expected to work on Linux w/ multi-headed setups, and this must be the height of the current screen.


Solution

  • More detailed background: the program I'm working on is a wallpaper manager. I want the (floating) window to use all the possible space vertically, and leave some space on both sides, to watch the background change.

    Having no reasonable solution, I decided to follow this answer.

    def check_height_and_start(window):
        w, h = window.get_size()
        window.destroy()
    
        if common.sway or common.env['wm'] == "i3":
            h = h * 0.95
    
        app = GUI(h)
    
    
    
    def main():
        (...)
        w = Gtk.Window() # actually I derived from Gtk.Window to make the window transparent
    
        if common.sway or common.env['wm'] == "i3":
            w.fullscreen()  # .maximize() doesn't work as expected on sway
        else:
            w.maximize()
    
        w.present()
        GLib.timeout_add(delay_ms, check_height_and_start, w)
        Gtk.main()
    

    This way the window height does not include panels, if any. Unfortunately it doesn't work on tiling window window managers, so I had to use fullscreen() instead of maximize(), and decrease height arbitrarily.

    The delay_ms optimal value varies with the hardware speed and WM in use, which is another inconvenience.