Donnie
Donnie

Reputation: 67

How to disable the close button in matplotlib

I used matplotlib to create a graphics window, but I do not want the user to manually close it. Is there a way to disable the closing button in the upper right corner? See screenshot

Upvotes: 3

Views: 2156

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

The solution will depend on the backend in use.

PyQt

For the PyQt backend, you can do the following:

import matplotlib
# make sure Qt backend is used
matplotlib.use("Qt4Agg")
from PyQt4 import QtCore  
import matplotlib.pyplot as plt

# create a figure and some subplots
fig, ax = plt.subplots(figsize=(4,2))
ax.plot([2,3,5,1])
fig.tight_layout()

win = plt.gcf().canvas.manager.window
win.setWindowFlags(win.windowFlags() | QtCore.Qt.CustomizeWindowHint)
win.setWindowFlags(win.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint)
plt.show()

This will disable the close button (not hide it).

enter image description here

Tk

I'm not sure if Tk is able to control the close button. But what is possible is to draw a completely frameless window.

import matplotlib
# make sure Tk backend is used
matplotlib.use("TkAgg")  
import matplotlib.pyplot as plt

# create a figure and some subplots
fig, ax = plt.subplots(figsize=(4,2))
ax.plot([2,3,5,1])
fig.tight_layout()

win = plt.gcf().canvas.manager.window
win.overrideredirect(1) # draws a completely frameless window

plt.show()

Upvotes: 3

Related Questions