psychopy.org | Reference | Downloads | Github

Gui text color accepts only color names? but not mentioned in documentation

Just a note. Seems that dialog boxes accept only color names, not RGB values, e.g.

dlg.addText(‘Subject Info’, color=‘Blue’)

but this is not mentioned in the documentation, it seems http://psychopy.org/api/gui.html

Alex

A colour value formatted like “#ff1122” works if you force the import of wx rather than Qt, but that has lots of other negative consequences…

Our code in qtgui.py is this (https://github.com/psychopy/psychopy/blob/master/psychopy/gui/qtgui.py#L121):

 def addText(self, text, color='', isFieldLabel=False):
        textLabel = QtWidgets.QLabel(text, parent=self)

        if len(color):
            palette = QtGui.QPalette()
            palette.setColor(QtGui.QPalette.Foreground, QtGui.QColor(color))
            textLabel.setPalette(palette)

i.e. we pass the colour value over to Qt to be interpreted, and according to this:

http://doc.qt.io/qt-5/qcolor.html#details

It should be able to deal with various formats. But instead, we get an error if we pass a list or tuple, and it fails silently for a string like “#ff1122”:

File “/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/demos/coder/input/GUI.py”, line 33, in
dlg.addText(‘Experiment Info’, color=(0,100,0,0))
File “/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/gui/qtgui.py”, line 126, in addText
palette.setColor(QtGui.QPalette.Foreground, QtGui.QColor(color))
TypeError: arguments did not match any overloaded call:
QColor(Qt.GlobalColor): argument 1 has unexpected type 'tuple’
QColor(int): too many arguments
QColor(QVariant): argument 1 has unexpected type 'tuple’
QColor(): argument 1 has unexpected type 'tuple’
QColor(int, int, int, alpha: int = 255): argument 1 has unexpected type ‘tuple’

Any clues from anyone else?

I’m not sure what exactly is going on but it might be worth raising a github issue about it so it can be tracked…

I don’t know for sure but I bet it needs to be, QColor(r, g, b) rather than what we’re doing which is effectively QColor( (r, g, b) ). So we pass a single tuple rather than three integers.
Our code would need to detect that color is a tuple and pass that as QColor( color[0], color[1], color[2])
But this is guesswork from looking at the code. I haven’t tested to see (and I didn’t write the qt dialog code)