在 PyQt 中设置工具提示的文本颜色 [英] Setting the text colour of a tooltip in PyQt

查看:51
本文介绍了在 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("\n", "<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.

关于如何在不使用 HTML 的情况下更改工具提示颜色或如何让基于 HTML 的工具提示不换行的任何建议?

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

谢谢

推荐答案

您可以使用样式表,例如:

You can use a stylesheet, for example:

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

更多信息请访问:

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

这篇关于在 PyQt 中设置工具提示的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆