如何禁用 matplotlib 窗口的最大化、最小化和关闭按钮? [英] How to disable the maximize, minimize and close button of the matplotlib window?

查看:134
本文介绍了如何禁用 matplotlib 窗口的最大化、最小化和关闭按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用matplotlib绘制图形,我想禁用窗口的最大化,最小化和关闭按钮.

I'm plotting a figure with matplotlib and I would like to disable the maximize, minimize and close button of the window.

可以吗?

推荐答案

matplotlib窗口是由使用中的后端创建的.不同的后端需要不同的解决方案.一个常见的后端是Qt后端,因此下面显示了该后端的两种解决方案.

The matplotlib window is created by a backend in use. Different backends require different solutions. A common backend is the Qt backend, so the following shows two solution for this backend.

您可以将 matplotlib 绘图嵌入到自定义 GUI 中,例如使用 PyQt.然后,您将获得GUI软件包提供的用于调整窗口的所有选项.

You can embed the matplotlib plot into a custom GUI, e.g. using PyQt. Then you have all the options the GUI package provides to adjust the window.

在 PyQt 中,可以通过将 QtCore.Qt.CustomizeWindowHint 设置为窗口标志来关闭窗口的顶部栏

In PyQt the window's top bar can be turned off via setting QtCore.Qt.CustomizeWindowHint as window flag

QMainWindow.setWindowFlags( QtCore.Qt.CustomizeWindowHint )

左边是普通版本,右边是自定义WindowFlag的版本.

Left is the normal version, right is the version with the custom WindowFlag.

完整示例:

import matplotlib.pyplot as plt
from PyQt4 import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar

class Window(QtGui.QMainWindow):
    def __init__(self, fig):
        self.qapp = QtGui.QApplication([])

        QtGui.QMainWindow.__init__(self)
        self.setWindowFlags( QtCore.Qt.CustomizeWindowHint )
        self.widget = QtGui.QWidget()
        self.setCentralWidget(self.widget)
        self.widget.setLayout(QtGui.QVBoxLayout())
        self.widget.layout().setContentsMargins(0,0,0,0)
        self.widget.layout().setSpacing(0)

        self.fig = fig
        self.canvas = FigureCanvas(self.fig)
        self.canvas.draw()

        self.nav = NavigationToolbar(self.canvas, self.widget)
        self.widget.layout().addWidget(self.nav)
        self.widget.layout().addWidget(self.canvas)

        self.show()
        exit(self.qapp.exec_()) 


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

# pass the figure to the custom window
a = Window(fig)

另一个选项是隐藏最小化和最大化按钮,而不是顶部栏.

Another option is to just hide the minimize and maximumize buttons, but not the top bar.

QMainWindow.setWindowFlags( QtCore.Qt.WindowCloseButtonHint )

如果您不想自己创建一个完整的窗口,则可以通过画布的父级获得该窗口.然后应用与上述相同的选项.为了使它起作用,您需要确保您实际上正在使用稍后要对其进行更改的后端.

If you don't want to create a complete window yourself you may obtain the window through the canvas' parent. Then the same options as above apply. In order for this to work you would need to make sure you are actually using the backend that you later want to make changes to.

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=(2.4,3))
ax.plot([2,3,5,1])
fig.tight_layout()


plt.gcf().canvas.parent().setWindowFlags( QtCore.Qt.CustomizeWindowHint )
plt.show()

这篇关于如何禁用 matplotlib 窗口的最大化、最小化和关闭按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆