-1

I recently made my first python and PyQt program and now I'm looking to play with colors to make it look good. I've been looking around the documentation and the QPalette documentation seems to suggest that a QPalette is the way to go as opposed to manually setting the colors for each widget. Problem is I can't find a whole lot of info out there on how to use this in practice and while there is certainly a lot of data in the documentation for a beginner like me it is lacking in many examples!

From what I understand my goal here is to establish within a QPalette object the sort of "global" colors for my app, and then I assign that palette to all of my widgets right? So what is the best way to go about this? For example I want to set all of my buttons to a dark gray background. Looking at the documentation it seems you need to set the QPalette.button() color using .setColor but I'm not able to get this to work (see line #83). But I was able to set the background color of the dialog specifically just above that.

Here's just the Gui part of my code:

import sys
from PyQt4 import QtGui, QtCore

class BatchTable(QtGui.QTableWidget):
    def __init__(self, parent):
        super(BatchTable, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setColumnCount(5)
        self.setColumnWidth(1,50)
        self.hideColumn(1)
        self.hideColumn(3)
        self.hideColumn(4)
        self.horizontalHeader().setStretchLastSection(True)
        self.setHorizontalHeaderLabels(QtCore.QString("Status;Alpha;File;Full Path;Process ID").split(";"))

class ffmpegBatch(QtGui.QWidget):
    def __init__(self):
        super(ffmpegBatch, self).__init__()
        self.initUI()

    def initUI(self):
        self.pBar = QtGui.QProgressBar()

        self.edit = QtGui.QTextEdit()

        cmdGroup = QtGui.QGroupBox("Commandline arguments")
        self.alphaCheck = QtGui.QCheckBox("Output alpha as separate file")
        fpsLbl = QtGui.QLabel("FPS:")
        self.fpsCombo = QtGui.QComboBox()
        self.fpsCombo.addItem("29.97")
        self.fpsCombo.addItem("23.976")
        hbox1 = QtGui.QHBoxLayout()
        hbox1.addWidget(self.alphaCheck)
        hbox1.addWidget(fpsLbl)
        hbox1.addWidget(self.fpsCombo)
        cmdGroup.setLayout(hbox1)

        saveGroup = QtGui.QGroupBox("Output")
        saveLocationBox = QtGui.QHBoxLayout()
        self.outputLocation = QtGui.QLineEdit()
        self.popBtn = QtGui.QPushButton("Pop dir")
        saveLocationBox.addWidget(self.outputLocation)
        saveLocationBox.addWidget(self.popBtn)

        saveBtnsBox = QtGui.QHBoxLayout()
        pasteFromClipboard = QtGui.QPushButton("Paste from clipboard")
        upOneBtn = QtGui.QPushButton("./")
        upTwoBtn = QtGui.QPushButton("././")
        saveBtnsBox.addWidget(pasteFromClipboard)
        saveBtnsBox.addWidget(upOneBtn)
        saveBtnsBox.addWidget(upTwoBtn)

        saveMasterBox = QtGui.QVBoxLayout()
        saveMasterBox.addLayout(saveLocationBox)
        saveMasterBox.addLayout(saveBtnsBox)

        saveGroup.setLayout(saveMasterBox)

        self.runBtn = QtGui.QPushButton("Run Batch Transcode")

        showDebugger = QtGui.QPushButton("Show debugger")
        showDebugger.setCheckable(True)

        self.mainBox = QtGui.QVBoxLayout()
        self.table = BatchTable(self)
        self.mainBox.addWidget(self.table)
        self.mainBox.addWidget(cmdGroup)
        self.mainBox.addWidget(saveGroup)
        self.mainBox.addWidget(self.runBtn)
        self.mainBox.addWidget(self.pBar)
        self.mainBox.addWidget(showDebugger)
        self.mainBox.addWidget(self.edit)
        self.edit.hide()

        self.setLayout(self.mainBox)
        self.setGeometry(300, 300, 600, 700)
        self.setWindowTitle('FFMPEG Batch Converter')

        # make pretty
        palette = QtGui.QPalette()
        palette.setColor(self.backgroundRole(), QtGui.QColor(40, 40, 40))
        self.setPalette(palette)
        palette.setColor(palette.button(), QtGui.QColor(100, 100, 100))
        self.runBtn.setPalette(palette)

def main():
    app = QtGui.QApplication(sys.argv)
    ex = ffmpegBatch()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
Spencer
  • 1,931
  • 1
  • 21
  • 44

1 Answers1

-1

Never mind, looks like stylesheets with cascading are a way better way to go. For example to set all buttons within the application to an absolutely beautiful yellow color: QApplication.setStyleSheet("QPushButton { background: yellow }")

Spencer
  • 1,931
  • 1
  • 21
  • 44