46 lines
1.2 KiB
Markdown
46 lines
1.2 KiB
Markdown
|
|
# PyQt 工具提示
|
|||
|
|
|
|||
|
|
> 原文: [https://pythonbasics.org/pyqt-tooltip/](https://pythonbasics.org/pyqt-tooltip/)
|
|||
|
|
|
|||
|
|
工具提示是将鼠标悬停在小部件上时显示的消息。
|
|||
|
|
|
|||
|
|
这可以是纯文本消息或格式消息(HTML)。 您可以通过在小部件上调用`.setToolTip("text")`来添加工具提示。 这通常用于协助用户。
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
## 工具提示示例
|
|||
|
|
|
|||
|
|
### PyQt 工具提示示例
|
|||
|
|
|
|||
|
|
下面的程序将工具提示消息添加到按钮。 它可以是纯文本或 HTML 格式的标签(标签为粗体和斜体)。
|
|||
|
|
|
|||
|
|
您可以在工具提示消息中设置所需的任何消息。
|
|||
|
|
|
|||
|
|
```py
|
|||
|
|
from PyQt5.QtWidgets import *
|
|||
|
|
import sys
|
|||
|
|
|
|||
|
|
class Window(QWidget):
|
|||
|
|
|
|||
|
|
def __init__(self):
|
|||
|
|
QWidget.__init__(self)
|
|||
|
|
layout = QGridLayout()
|
|||
|
|
self.setLayout(layout)
|
|||
|
|
|
|||
|
|
button = QPushButton("Button")
|
|||
|
|
button.setToolTip("This is a text")
|
|||
|
|
layout.addWidget(button, 0, 0)
|
|||
|
|
|
|||
|
|
button = QPushButton("Button")
|
|||
|
|
button.setToolTip("<b>HTML</b> <i>can</i> be shown too..")
|
|||
|
|
layout.addWidget(button, 1, 0)
|
|||
|
|
|
|||
|
|
app = QApplication(sys.argv)
|
|||
|
|
screen = Window()
|
|||
|
|
screen.show()
|
|||
|
|
sys.exit(app.exec_())
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
[下载示例](https://gum.co/pysqtsamples)
|