Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

python - Setting the text colour of a tooltip in PyQt

I've been adding tooltips to an app that I've been writing, and had issues with the colour of the tooltip text.

The app has a number of buttons, which change background and text colour depending on what the status of the button is. The text is either white or black.

The tooltip text colour always seems to follow the colour of the button text. When the text on the button is white, then it's all fine, but if the text on the button is black, then the tooltip is black text on a black background. What I want to be able to have is the tooltip to always be white text on black, whatever the button's colour.

For this, I was setting my tooltip using:

self.setToolTip(toolTip)

Now I did find a potential solution to this, but it had its own issues:

self.setToolTip("<font color=white>%s</font>" % toolTip.replace("
", "<br/>"))

However, when I do this, the lines in my tooltip start wrapping, rather than keeping the full line length on one line, which is what I want it to do.

Any suggestions on either how to change the tooltip colour without using HTML, or how to get the HTML-based tooltip to not wrap?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a stylesheet, for example:

self.setStyleSheet("""QToolTip { 
                           background-color: black; 
                           color: white; 
                           border: black solid 1px
                           }""")

There is more information available at:

http://doc.qt.io/qt-4.8/stylesheet-examples.html#customizing-qtooltip


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...