2

I would like to create some clickable blue text on my GUI, kind of like a HTML hyperlink! I am using Python 2.6 and PyQt4. I have managed to set the text colour of a QLabel before, but cannot remember how i did that, even if i did its not clickable. So I have moved onto a QPushButton that I can make flat like so:

testbutton = qt.QPushButton("Test")
testbutton.setFlat(True)

So far all my searches have turned up c++ and c# methods and i cant seem to find a python equivalent!

Any ideas on how to change the text colour or even different ways of accomplishing this entirely would be most welcome!

3 Answers 3

7

You can use a Style Sheet.

testbutton.setStyleSheet('QPushButton {color: blue}')
Sign up to request clarification or add additional context in comments.

Comments

3

You can use the palette property of your QPushButton and apply your blue color to its ButtonText color role:

testbutton = qt.QPushButton("Test")
testbutton.setFlat(True)

palette = qt.QPalette(testbutton.palette()) # make a copy of the palette
palette.setColor(qt.QPalette.ButtonText, qt.QColor('blue'))
testbutton.setPalette(palette) # assign new palette

2 Comments

Also worked perfectly, but as the other solution was a bit simpler using only one line of code i think i will use that, unless there is a reason why using the palette option is a better idea? or is it just a matter of preference?
I think the palette can be seen as the 'old style' solution. In my opinion, the more flexible way is to use a css file and assign its content to the QApplication: myQApp.setStyleSheet(open(myCssFile).read()). By doing so you can change the look and feel of your application without having to modify a line of code. Qt's Css provides various ways to adress widgets; see Selector Types
0
testbutton.setStyleSheet("background-color: #00FF00; color: #00FF00;")

where: "background-color" is color of You button. "color" is color of the text of button.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.