Skip to content

Use rcParams to control default "raise window" behavior (Qt,Gtk,Tk,Wx) #15440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:orphan:

rcParams for controlling default "raise window" behavior
--------------------------------------------------------
The new config option ``rcParams['figure.raise_window']`` allows to disable
raising the plot window when calling ``plt.show`` or ``plt.pause`` methods.
``MacOSX`` backend is currently not supported.

6 changes: 3 additions & 3 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ def destroy(*args):
self.window.deiconify()
else:
self.canvas.draw_idle()
# Raise the new window.
self.canvas.manager.window.attributes('-topmost', 1)
self.canvas.manager.window.attributes('-topmost', 0)
if matplotlib.rcParams['figure.raise_window']:
self.canvas.manager.window.attributes('-topmost', 1)
self.canvas.manager.window.attributes('-topmost', 0)
self._shown = True

def destroy(self, *args):
Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ def destroy(self, *args):
def show(self):
# show the figure window
self.window.show()
self.window.present()
self.canvas.draw()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does GTK3 need a draw now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I am testing with Gtk3 and turn off the "raise window", I encountered a strange behavior, that the GUI window is reluctant to update, and the whole program get stuck, loop is not running, until I move/switch the GUI window or moving my mouse cursor on the canvas, it can continue to run/update.
After debug and tracing, I found the program is stuck inside the Gtk.main_iteration() here:

    def flush_events(self):
        # docstring inherited
        Gdk.threads_enter()
        while Gtk.events_pending():
            Gtk.main_iteration()
        Gdk.flush()
        Gdk.threads_leave()

By looking into the doc, it says if there is no event queued, main_iteration will block until the next event is noticed, but that's very strange since we have already checked with events_pending, if there is no event it should not go into the while-loop. Nevertheless, I changed using Gtk.gtk_main_iteration_do(False) which the doc states that it won't block, however, that's not working, the program still get stuck inside gtk_main_iteration_do. Finally, I come up with a solution: to call window.queue_draw() right after window.show() and successfully I don't have to waving my mouse cursor over the canvas any more. And I found that this is exactly what canvas.draw() is doing, and the same approach is also used in the backend_wx.py, so I suppose this is the right way to do it? I am not familiar with the GUI stuff so I have no idea what's happening, please feel free to correct me.
This is the full report, HaHa!! Thank you so much for your patience reading this mumbo jumbo!! 😜

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you test with draw_idle instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested, won't update, need stick to draw

if matplotlib.rcParams['figure.raise_window']:
self.window.present()

def full_screen_toggle(self):
self._full_screen_flag = not self._full_screen_flag
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,9 @@ def resize(self, width, height):

def show(self):
self.window.show()
self.window.activateWindow()
self.window.raise_()
if matplotlib.rcParams['figure.raise_window']:
self.window.activateWindow()
self.window.raise_()

def destroy(self, *args):
# check for qApp first, as PySide deletes it in its atexit handler
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,8 @@ def __init__(self, canvas, num, frame):
def show(self):
self.frame.Show()
self.canvas.draw()
if matplotlib.rcParams['figure.raise_window']:
self.frame.Raise()

def destroy(self, *args):
_log.debug("%s - destroy()", type(self))
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ def _validate_linestyle(ls):
'figure.frameon': [True, validate_bool],
'figure.autolayout': [False, validate_bool],
'figure.max_open_warning': [20, validate_int],
'figure.raise_window': [True, validate_bool],

'figure.subplot.left': [0.125, _range_validators["0 <= x <= 1"]],
'figure.subplot.right': [0.9, _range_validators["0 <= x <= 1"]],
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/style/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
'interactive', 'backend', 'backend.qt4', 'webagg.port', 'webagg.address',
'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback',
'toolbar', 'timezone', 'datapath', 'figure.max_open_warning',
'savefig.directory', 'tk.window_focus', 'docstring.hardcopy'}
'figure.raise_window', 'savefig.directory', 'tk.window_focus',
'docstring.hardcopy'}


def _remove_blacklisted_style_params(d, warn=True):
Expand Down
1 change: 1 addition & 0 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@
#figure.max_open_warning : 20 ## The maximum number of figures to open through
## the pyplot interface before emitting a warning.
## If less than one this feature is disabled.
#figure.raise_window : True ## Raise the GUI window to front when show() is called

## The figure subplot parameters. All dimensions are a fraction of the figure width and height.
#figure.subplot.left : 0.125 ## the left side of the subplots of the figure
Expand Down