PyQt5 – 设置和访问状态栏的名称

PyQt5 – 设置和访问状态栏的名称

在设计一个Gui(图形用户界面)应用程序时,我们通常会制作很多窗口,每个窗口都有状态栏。对于后端代码员来说,区分窗口的状态栏变得很困难。为了克服这个需要设置名称的问题,在数百个状态栏中,我们可以通过设置它们的名称来区分它们,比如有些是 “状态-信息 “状态栏,有些是 “状态警告 “状态栏,就像这样。

为了设置状态栏的名称,我们使用 statusBar().setAccessibleName() 方法,为了访问这个名称,我们使用 statusBar().accessibleName() 方法。

语法:

self.statusBar().setAccessibleName(name)
statusBar().accessibleName()

参数:

statusBar().setAccessibleName()使用字符串作为参数。

statusBar().accessibleName()不使用参数。

返回 :

statusBar().setAccessibleName()返回 None。

statusBar().setAccessibleName()返回 string。

代码 :

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 name to a status bar
        self.statusBar().setAccessibleName("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;")
  
        # getting name of status bar
        name = self.statusBar().accessibleName()
  
        # setting text to label
        self.label_1.setText("name = "+name)
  
        # 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())

输出 :

PyQt5 - 设置和访问状态栏的名称

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程