简体   繁体   中英

How to set PyQT4 Stylesheet for Matplotlib widget (navBar/canvas)?

I have a QMainWindow app consisting of a Menu Bar, Splitter in the central widget, and a status bar. On the left side of the splitter is a widget containing some controls (list, combo, and button) and on the right is a widget containing a layout of Matplotlib canvas, and NavigationToolBar.

I was able to use QT Stylesheets to set the various background colors of everything ... except the Matplotlib side. I attempted to use the following but it has no effect. I also tried setting the NavBar and Canvas stylesheets directly ... but again it didn't work:

self.mplFig.setStyleSheet(".QWidget {background-color:   #0ff}")

Basically I am trying to turn the background color of the NavToolbar and the border around the canvas to match everything else (Currently cyan so it shows up easy) ... Any help would be appreciated. Full code & sample image below:

import sys
from PyQt4 import QtGui, QtCore
import matplotlib
matplotlib.use("qt4agg")
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

class testGUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(testGUI, self).__init__(parent)
        self.buildLayout()
        self.buildMenus()        
        self.menuBar()
        self.statusBar()

        ## Style Sheets
        self.splitter.setStyleSheet("QSplitter::handle:horizontal {background-color:   #ccc}")            
        self.controlWidget.setStyleSheet(".QWidget {background-color:   #0ff}")  
        menuStyle = """.QMenuBar {background-color:   #0ff}
            QMenuBar::item {background: transparent} 
            QMenuBar::item:selected {background: #8ff}"""
        self.statusBar().setStyleSheet(".QStatusBar {background-color:   #0ff}")
        self.menuBar().setStyleSheet(menuStyle)
        # .....THIS DOESN"T WORK !! .....
        self.mplFig.setStyleSheet(".QWidget {background-color:   #0ff}")


    def buildLayout(self):
        self.controlWidget = QtGui.QWidget(self) 
        self.plotList  = QtGui.QListWidget(self)
        self.combo  = QtGui.QComboBox(self)
        self.button = QtGui.QPushButton('Plot')        
        self.combo.addItems(\['1','2','3','4'\]) 
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.plotList)
        layout.addWidget(self.combo)
        layout.addWidget(self.button)
        self.controlWidget.setLayout(layout)
        self.mplFig  = MplGrapher()
        self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        self.splitter.addWidget(self.controlWidget) 
        self.splitter.addWidget(self.mplFig) 
        self.setCentralWidget(self.splitter)
        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Plastique'))
    def buildMenus(self):
        openFile = QtGui.QAction('Open', self)
        self.fileMenu = self.menuBar().addMenu('&File')
        self.fileMenu.addAction(openFile)

class MplGrapher(QtGui.QWidget):
    def __init__(self,parent=None):
        super(MplGrapher, self).__init__(parent)
        self.initFigure()

    def initFigure(self):   
        self.figure = Figure()        
        self.canvas = FigureCanvas(self.figure)
        self.navbar = NavigationToolbar(self.canvas, self) 
        self.figure.add_subplot(1,1,1)
        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.navbar)
        self.layout.addWidget(self.canvas)
        self.setLayout(self.layout)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = testGUI()
    main.show()
    sys.exit(app.exec_())

示例GUI

I think I may have a solution thanks to some tips from Maxwell Grady.

I changed the following 2 lines:

self.mplFig.setStyleSheet("QWidget {background-color:   #0ff}")

Note the lack of a "." before QWidget and

class MplGrapher(QtGui.QGroupBox):

Picture of GUI after changes. Now that I can actually set some style properties ... time to make it less ugly.

边框颜色的样本。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM