简体   繁体   中英

Get background color of QTabWidget in PyQt5

When creating a PyQt5 tab widget the background colour is whiter than the background of a normal widget. I am looking for a way to get the tab exact background colour.

Some good examples that relate to this question are:

Get the background color of a widget - really

How to make the background color the same as the form background?

The most common suggestions to get the background colour is to use:

widget.palette().color(QtGui.QPalette.Background)
# alternatively with QtGui.QPalette.Base

None of which gets the exact colour. The first being too dark and the later too white.

Whether this works on not also depends on the style and system you are using. On my case it is on a Linux Mint setup but the app is also intended for windows.

===== Using this to set the background of a matplotlib figure =====

The use case for this question is to keep the facecolor of a matplotlib figure consistent with the widget it is embed on.

Here is my example:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib as mpl


class Window(QtWidgets.QMainWindow):
    """ Class to use the data pattern widget in a separate window"""
    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)

        # ===== Setup the window =====
        self.setWindowTitle("background")
        self.resize(600, 400)

        self.maintabs = QtWidgets.QTabWidget(self)
        self.setCentralWidget(self.maintabs)

        self.page = QtWidgets.QWidget(self)
        self.mpl_layout = QtWidgets.QHBoxLayout(self.page)
        self.maintabs.addTab(self.page, 'Demo tab')

        # ===== Set up matplotlib canvas =====
        self.mpl_canvas = None
        # get background color from widget and convert it to RBG
        pyqt_bkg = self.maintabs.palette().color(QtGui.QPalette.Background).getRgbF()
        mpl_bkg = mpl.colors.rgb2hex(pyqt_bkg)

        self.pltfig = mpl.figure.Figure()
        self.pltfig.set_facecolor(mpl_bkg)  # uses the background of mainwindow and not tab
        self.plot_ax = self.pltfig.add_subplot(111)
        self.addmpl(self.pltfig)

    def addmpl(self, fig):
        self.mpl_canvas = FigureCanvas(fig)
        self.mpl_layout.addWidget(self.mpl_canvas)
        self.mpl_canvas.draw()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec())

Resulting in,

结果 gui

The colors of a palette are only a reference for how the style will actually paint widgets. Some widgets use gradients to show their contents (consider buttons, which usually "glow" the color given for the palette Button role). Some styles even paint some widgets using roles that seem unrelated to that type of widget.

QTabWidget behaves in a similar way (depending on the style), so even if you get its background, it will probably painted with a slightly different color.

A possible solution is to set the background of the widget using stylesheet, while still keeping the palette reference:

    self.maintabs.setStyleSheet('background: palette(window);')

Note that this will make all children widgets of maintabs use that plain background color.

you can use stylesheet to set the background-color, like these:

self.maintabs.setStyleSheet("background-color: rgb(0,0,59)")

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