【问题标题】:Python pyqt5 make font of settooltip different than button fontPython pyqt5使settooltip的字体与按钮字体不同
【发布时间】:2019-08-19 14:06:45
【问题描述】:

如何让工具提示中文本的字体大小和颜色与按钮的不同?它一直显示为按钮的大小/字体,而不是它自己的。另外,如何让文本“离开程序”真正适合工具提示?

我已经尝试过这种方法,但无法让它发挥作用: Setting the text colour of a tooltip in PyQt

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *  

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(100, 100)
        self.setWindowTitle("Example")

        self.leave = QPushButton("X", self)
        self.leave.setStyleSheet("background-color : grey ; color : red ; font: 20pt")
        self.leave.setToolTip("Leave the Program")
        self.setStyleSheet("QToolTip{background-color : blue ; color: k ; font: 12pt}")
        self.leave.move(38, 25)
        self.leave.resize(24, 50)
        self.leave.clicked.connect(self.exit)

    def exit(self):
        app.quit()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow() 
    w.show()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    有问题的工具提示是按钮的子按钮,而不是主窗口的子按钮,因此它将继承按钮的样式表(以及按钮祖先的样式表)。由于您没有在按钮的样式表中指定选择器,因此此样式表中的样式规则将应用于所有按钮的子项,包括工具提示(除非它们自己有样式表)。解决此问题的一种方法是将按钮的样式表限制为 QPushButton 对象,仅通过执行类似的操作

    self.leave.setStyleSheet("QPushButton{background-color : grey ; color : red ; font: 20pt}")
    

    此外,要使工具提示的背景颜色从默认值更改,我需要为其边框指定样式规则,例如

    self.setStyleSheet(" QToolTip{ border: 1px solid white; background-color: blue ; color: k ; font: 12pt}")
    

    我不确定这是错误还是设计。

    截图:

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多