【问题标题】:PyQt5 change app theme/palette dynamicallyPyQt5 动态更改应用程序主题/调色板
【发布时间】:2018-09-12 06:19:45
【问题描述】:

我可以成功地创建一个新的调色板并在启动时将它设置为 QApplication。但是,一旦应用程序运行,我就不能再更改调色板的颜色了。如果用户选择特定主题,我想更改按钮的颜色。当前在启动时有效但以后不可用的代码如下:

palette = QtGui.QPalette()
palette.setColor(QtGui.QPalette.Window, QtGui.QColor(3, 18, 14))
palette.setColor(QtGui.QPalette.Base, QtGui.QColor(15, 15, 15))
palette.setColor(QtGui.QPalette.AlternateBase, QtGui.QColor(53, 53, 53))
palette.setColor(QtGui.QPalette.ToolTipBase, QtCore.Qt.white)
palette.setColor(QtGui.QPalette.Text, QtCore.Qt.white)
palette.setColor(QtGui.QPalette.Button, QtGui.QColor(53, 53, 53))
palette.setColor(QtGui.QPalette.ButtonText, QtCore.Qt.white)
palette.setColor(QtGui.QPalette.BrightText, QtCore.Qt.red)
palette.setColor(QtGui.QPalette.Highlight, QtGui.QColor(142, 45, 197).lighter())
palette.setColor(QtGui.QPalette.HighlightedText, QtCore.Qt.black)
app.setPalette(palette)

如果在插槽中使用相同的调色板对象不会改变颜色。例如槽中的代码:

palette.setColor(QtGui.QPalette.Button, QtGui.QColor(53, 53, 53))
app.setPalette(palette)

有人可以告诉我我在这里缺少什么吗? 谢谢。

编辑: 我刚刚发现它只修改了一些区域。例如,在我编写的插槽中,它会更改高亮颜色,但不会更改 QTab 颜色(通过将颜色设置为 Button 来设置)。我当前的插槽代码:

def change_theme(self): 
    pal = QtWidgets.QApplication.palette() 
    #The next line works 
    pal.setColor(QtGui.QPalette.Highlight, QtGui.QColor(0, 0, 128)) 
    #The next line doesnt work. Expected it to change the Tab Widget color 
    #using this line. 
    pal.setColor(QtGui.QPalette.Button, QtGui.QColor(62, 80, 91)) 
    QtWidgets.QApplication.setPalette(pal)

【问题讨论】:

    标签: python python-3.x pyqt pyqt4 pyqt5


    【解决方案1】:

    尝试设置app.setStyle('Fusion')

    from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QApplication
    from PyQt5.QtGui import QPalette, QColor
    
    class Window(QWidget):
    
        def __init__(self):
            super().__init__()
            self.flag = False
    
            self.button = QPushButton('change the colors of the buttons', self)
            self.button.clicked.connect(self.click)
            lay = QVBoxLayout(self)
            lay.addWidget(self.button)
    
            self.palette = self.palette()
            self.palette.setColor(QPalette.Window, QColor(3, 18, 14))
    
            self.palette.setColor(QPalette.Button, QColor('red'))  
    
            self.setPalette(self.palette)
    
        def click(self):
            print("click")
            if not self.flag:
                self.palette.setColor(QPalette.Button, QColor(62, 80, 91))
            else: 
                self.palette.setColor(QPalette.Button, QColor(0, 0, 128))
    
            self.setPalette(self.palette)
            self.flag = not self.flag
    
    
    if __name__ == '__main__':
        import sys
        app = QApplication([])
    
        app.setStyle('Fusion')                              # <-----
    
        w = Window()
        w.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2016-12-31
      • 1970-01-01
      • 2012-03-30
      • 2016-11-01
      相关资源
      最近更新 更多