简体   繁体   English

获取PyQt5中QTabWidget的背景颜色

[英]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.创建 PyQt5 选项卡小部件时,背景颜色比普通小部件的背景更白。 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.就我而言,它是在 Linux Mint 设置上,但该应用程序也适用于 windows。

===== Using this to set the background of a matplotlib figure ===== ===== 用这个来设置一个matplotlib图的背景=====

The use case for this question is to keep the facecolor of a matplotlib figure consistent with the widget it is embed on.这个问题的用例是保持 matplotlib 图形的面颜色与其嵌入的小部件一致。

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.调色板的 colors 只是样式如何实际绘制小部件的参考 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.一些 styles 甚至使用看起来与该类型小部件无关的角色来绘制一些小部件。

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. QTabWidget 的行为方式类似(取决于样式),因此即使您获得了它的背景,它也可能会涂上稍微不同的颜色。

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.请注意,这将使maintabs所有子小部件都使用该纯背景颜色。

you can use stylesheet to set the background-color, like these:您可以使用样式表来设置背景颜色,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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