PyQt5 – 设置和访问状态栏的WHATS THIS帮助文本
在PyQt5中,有很多小部件和条形图,当我们倾向于制作一个应用程序时,我们最终会放上很多窗口,每个窗口都有退出状态栏。有时我们不记得为什么要使用这个状态栏,这个状态栏是什么。这就是为什么PyQt5允许我们为后端设置帮助文本。在这篇文章中,我们将看到如何设置和访问状态栏的帮助文本。
为了设置帮助文本,我们使用setWhatsThis()方法,为了访问帮助文本,我们使用whatsThis()方法。
语法:
self.statusBar().setWhatsThis()
self.statusBar().whatsThis()
参数:
setWhatsThis()以字符串为参数。
whatsThis()没有参数。
返回 :
setWhatsThis() 返回无
whatsThis() 返回字符串。
代码 。
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# set the title
self.setWindowTitle("Python")
# setting the geometry of window
self.setGeometry(60, 60, 600, 400)
# setting status bar message
self.statusBar().showMessage("This is status bar")
# setting border
self.statusBar().setStyleSheet("border :3px solid black;")
# setting help text
self.statusBar().setWhatsThis("this is help text for status bar")
# creating a label widget
self.label_1 = QLabel(self)
# moving position
self.label_1.move(100, 100)
# setting up the border
self.label_1.setStyleSheet("border :1px solid blue;")
# accessing help text of status bar
help = self.statusBar().whatsThis()
# setting text to label
self.label_1.setText(help)
# resizing label
self.label_1.adjustSize()
# show all the widgets
self.show()
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :