简体   繁体   English

使用 Python 在 PyQt4 中的 QPushButton 中设置文本的颜色

[英]Set the colour of text in a QPushButton in PyQt4 using Python

I would like to create some clickable blue text on my GUI, kind of like a HTML hyperlink.我想在我的 GUI 上创建一些可点击的蓝色文本,有点像 HTML 超链接。 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.我正在使用 Python 2.6 和 PyQt4,我之前设法设置了 QLabel 的文本颜色,但不记得我是怎么做到的。 even if i did its not clickable: So I have moved onto a QPushButton that I can make flat like so:即使我做了它不可点击:所以我已经转移到一个 QPushButton 上,我可以像这样将它做成平面:

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!到目前为止,我所有的搜索都出现了 c++ 和 c# 方法,我似乎找不到 python 等效方法!

Any ideas on how to change the text colour or even different ways of accomplishing this entirely would be most welcome!任何关于如何更改文本颜色甚至完全实现此目的的不同方法的想法都将受到欢迎!

You can use a Style Sheet .您可以使用样式表

testbutton.setStyleSheet('QPushButton {color: blue}')

You can use the palette property of your QPushButton and apply your blue color to its ButtonText color role:您可以使用QPushButtonpalette属性并将蓝色应用于其ButtonText颜色角色:

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
testbutton.setStyleSheet("background-color: #00FF00; color: #00FF00;")

where: "background-color" is color of You button.其中:“background-color”是 You 按钮的颜色。 "color" is color of the text of button. “颜色”是按钮文本的颜色。

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

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