Created
November 26, 2025 06:03
-
-
Save biochem-fan/df3adcb033a90cf31e148230e505279d to your computer and use it in GitHub Desktop.
Force X Window repaint upon resize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is a hacky workaround for the XQuartz repaint bug reported in | |
| # https://github.com/XQuartz/XQuartz/issues/438. | |
| # This requires python-xlib. | |
| # | |
| # FIXME: | |
| # - I don't know how to get rid of BadMatch errors. | |
| # - Sometimes you need to explicitly set DISPLAY as: | |
| # DISPLAY=:0 python repaint-upon-resize.py | |
| # - Ctrl-C does not work even though I catch KeyboardInterrupt. | |
| import sys | |
| from Xlib import X, display, error | |
| def redraw_window_recursively(window, d): | |
| try: | |
| attrs = window.get_attributes() | |
| if attrs.map_state == X.IsViewable: | |
| # print("Repaint window", window) | |
| window.clear_area(x=0, y=0, width=0, height=0, exposures=True) | |
| tree = window.query_tree() | |
| for child in tree.children: | |
| redraw_window_recursively(child, d) | |
| except error.XError: | |
| pass | |
| def detect_window_resize(): | |
| try: | |
| d = display.Display() | |
| except error.DisplayError: | |
| print("Cannot connect to the X server.", file=sys.stderr) | |
| sys.exit(1) | |
| root = d.screen().root | |
| event_mask = X.SubstructureNotifyMask | X.StructureNotifyMask | |
| root.change_attributes(event_mask=event_mask) | |
| while True: | |
| try: | |
| event = d.next_event() | |
| if event.type == X.ConfigureNotify: | |
| window_id = event.window.id | |
| window = d.create_resource_object('window', window_id) | |
| redraw_window_recursively(window, d) | |
| except KeyboardInterrupt: | |
| break | |
| except: | |
| continue | |
| if __name__ == "__main__": | |
| detect_window_resize() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oops. let me retract the confirmation. If I add the lines to return early then I don't see my debug message saying it is repainting the window at new line 22 so it's skipping everything.