{"id":5197,"date":"2022-10-11T00:37:08","date_gmt":"2022-10-11T00:37:08","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5197"},"modified":"2022-10-11T00:37:10","modified_gmt":"2022-10-11T00:37:10","slug":"pyqt-qstatusbar","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qstatusbar\/","title":{"rendered":"PyQt QStatusBar"},"content":{"rendered":"\n<p>Summary: in this tutorial, you&#8217;ll learn how to use the PyQt <code>QStatusBar<\/code> class to create status bar widgets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qstatusbar-class'>Introduction to the PyQt QStatusBar class <a href=\"#introduction-to-the-pyqt-qstatusbar-class\" class=\"anchor\" id=\"introduction-to-the-pyqt-qstatusbar-class\" title=\"Anchor for Introduction to the PyQt QStatusBar class\">#<\/a><\/h2>\n\n\n\n<p>The <code>QStatusBar<\/code> class allows you to create a status bar widget. The status bar widget is useful for presenting status information.<\/p>\n\n\n\n<p>Typically, you&#8217;ll use the <code>statusBar()<\/code> method of the <code>QMainWindow<\/code> object to create a status bar for the main window:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.status_bar = self.statusBar()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>statusBar()<\/code> method returns the status bar of the main window. If the status bar doesn&#8217;t exist, the function creates and returns an empty status bar.<\/p>\n\n\n\n<p>So if you assign the status bar to the <code>self.status_bar<\/code> variable, you can use the variable later in other methods of the class.<\/p>\n\n\n\n<p>Or you can directly call the <code><code>self.statusBar<\/code>()<\/code> method to get the <code>QStatusBar<\/code> object and call its method.<\/p>\n\n\n\n<p>To show a message on the status bar, you use the <code>showMessage()<\/code> method of the <code>QStatusBar<\/code> object.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">showMessage(text,timeout=<span class=\"hljs-number\">0<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By default, the <code><code>showMessage()<\/code><\/code> message will display the text until you call the <code>clearMessage()<\/code> or the <code><code>showMessage()<\/code><\/code> method again.<\/p>\n\n\n\n<p>The timeout specifies the number of milliseconds the message will display. The timeout defaults to zero which displays the text permanently.<\/p>\n\n\n\n<p>Each status on the status has one of the following categories:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Temporary &#8211; briefly occupies most of the status bar. For example, statuses that explain tool tip texts or menu entries. <\/li><li>Normal &#8211; occupies only part of the status bar. For example, the status for displaying the page and line number.<\/li><li>Permanent &#8211; is always shown. For example, a message that shows a Caps Lock indicator status.<\/li><\/ul>\n\n\n\n<p>In addition to showing a message, you can add a widget to the status bar using the <code>addWidget()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">addWidget(widget&#91;, stretch=<span class=\"hljs-number\">0<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To hide a widget on the status bar, you use the <code>removeWidget()<\/code> method. Once you hide a widget, you can display it again using the <code>addWidget()<\/code> method.<\/p>\n\n\n\n<p>The status bar locates the widget added by the <code>addWidget()<\/code> method at the far left. If you call the <code>showMessage()<\/code> method, the message will hide the widget.<\/p>\n\n\n\n<p>To add a permanent widget to the status bar, you use <code>addPermanentWidget()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">addPermanentWidget(widget&#91;, stretch=<span class=\"hljs-number\">0<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The method adds the widget to the status bar and changes the parent of the widget to the <code><code>QStatusBar<\/code><\/code> object if the widget is not a child of the <code><code>QStatusBar<\/code><\/code> object.<\/p>\n\n\n\n<p>The <code>QStatusBar<\/code> locates the permanent widget to the far right of the status bar.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='pyqt-qstatusbar-example'>PyQt QStatusBar example <a href=\"#pyqt-qstatusbar-example\" class=\"anchor\" id=\"pyqt-qstatusbar-example\" title=\"Anchor for PyQt QStatusBar example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll continue the text editor program from the <code>QToolBar<\/code> tutorial. We&#8217;ll do the following with the status bar: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Show a <code>Ready<\/code> message that displays in 5 seconds when the program launches.<\/li><li>Add a permanent widget that displays the number of characters on the status bar.<\/li><li>Show a message when the file has been saved.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"752\" height=\"496\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QStatusBar-Status-Bar-Example.png\" alt=\"\" class=\"wp-image-5201\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QStatusBar-Status-Bar-Example.png 752w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QStatusBar-Status-Bar-Example-300x198.png 300w\" sizes=\"auto, (max-width: 752px) 100vw, 752px\" \/><\/figure>\n\n\n\n<p>Here&#8217;s the complete program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> pathlib <span class=\"hljs-keyword\">import<\/span> Path\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QMainWindow, QTextEdit, QFileDialog, QMessageBox, QToolBar, QLabel\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtGui <span class=\"hljs-keyword\">import<\/span> QIcon, QAction\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> QSize\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QMainWindow)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, *args, **kwargs)<\/span>:<\/span>\n        super().__init__(*args, **kwargs)\n\n        self.setWindowIcon(QIcon(<span class=\"hljs-string\">'.\/assets\/editor.png'<\/span>))\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">500<\/span>, <span class=\"hljs-number\">300<\/span>)\n\n        self.title = <span class=\"hljs-string\">'Editor'<\/span>\n        self.filters = <span class=\"hljs-string\">'Text Files (*.txt)'<\/span>\n\n        self.set_title()\n\n        self.path = <span class=\"hljs-literal\">None<\/span>\n\n        self.text_edit = QTextEdit(self)\n        self.text_edit.textChanged.connect(self.text_changed)\n        self.setCentralWidget(self.text_edit)        \n\n        menu_bar = self.menuBar()\n\n        file_menu = menu_bar.addMenu(<span class=\"hljs-string\">'&amp;File'<\/span>)\n        edit_menu = menu_bar.addMenu(<span class=\"hljs-string\">'&amp;Edit'<\/span>)\n        help_menu = menu_bar.addMenu(<span class=\"hljs-string\">'&amp;Help'<\/span>)\n\n        <span class=\"hljs-comment\"># new menu item<\/span>\n        new_action = QAction(QIcon(<span class=\"hljs-string\">'.\/assets\/new.png'<\/span>), <span class=\"hljs-string\">'&amp;New'<\/span>, self)\n        new_action.setStatusTip(<span class=\"hljs-string\">'Create a new document'<\/span>)\n        new_action.setShortcut(<span class=\"hljs-string\">'Ctrl+N'<\/span>)\n        new_action.triggered.connect(self.new_document)\n        file_menu.addAction(new_action)\n\n        <span class=\"hljs-comment\"># open menu item<\/span>\n        open_action = QAction(QIcon(<span class=\"hljs-string\">'.\/assets\/open.png'<\/span>), <span class=\"hljs-string\">'&amp;Open...'<\/span>, self)\n        open_action.triggered.connect(self.open_document)\n        open_action.setStatusTip(<span class=\"hljs-string\">'Open a document'<\/span>)\n        open_action.setShortcut(<span class=\"hljs-string\">'Ctrl+O'<\/span>)\n        file_menu.addAction(open_action)\n\n        <span class=\"hljs-comment\"># save menu item<\/span>\n        save_action = QAction(QIcon(<span class=\"hljs-string\">'.\/assets\/save.png'<\/span>), <span class=\"hljs-string\">'&amp;Save'<\/span>, self)\n        save_action.setStatusTip(<span class=\"hljs-string\">'Save the document'<\/span>)\n        save_action.setShortcut(<span class=\"hljs-string\">'Ctrl+S'<\/span>)\n        save_action.triggered.connect(self.save_document)\n        file_menu.addAction(save_action)\n\n        file_menu.addSeparator()\n\n        <span class=\"hljs-comment\"># exit menu item<\/span>\n        exit_action = QAction(QIcon(<span class=\"hljs-string\">'.\/assets\/exit.png'<\/span>), <span class=\"hljs-string\">'&amp;Exit'<\/span>, self)\n        exit_action.setStatusTip(<span class=\"hljs-string\">'Exit'<\/span>)\n        exit_action.setShortcut(<span class=\"hljs-string\">'Alt+F4'<\/span>)\n        exit_action.triggered.connect(self.quit)\n        file_menu.addAction(exit_action)\n\n        <span class=\"hljs-comment\"># edit menu<\/span>\n        undo_action = QAction(QIcon(<span class=\"hljs-string\">'.\/assets\/undo.png'<\/span>), <span class=\"hljs-string\">'&amp;Undo'<\/span>, self)\n        undo_action.setStatusTip(<span class=\"hljs-string\">'Undo'<\/span>)\n        undo_action.setShortcut(<span class=\"hljs-string\">'Ctrl+Z'<\/span>)\n        undo_action.triggered.connect(self.text_edit.undo)\n        edit_menu.addAction(undo_action)\n\n        redo_action = QAction(QIcon(<span class=\"hljs-string\">'.\/assets\/redo.png'<\/span>), <span class=\"hljs-string\">'&amp;Redo'<\/span>, self)\n        redo_action.setStatusTip(<span class=\"hljs-string\">'Redo'<\/span>)\n        redo_action.setShortcut(<span class=\"hljs-string\">'Ctrl+Y'<\/span>)\n        redo_action.triggered.connect(self.text_edit.redo)\n        edit_menu.addAction(redo_action)\n\n        about_action = QAction(QIcon(<span class=\"hljs-string\">'.\/assets\/about.png'<\/span>), <span class=\"hljs-string\">'About'<\/span>, self)\n        help_menu.addAction(about_action)\n        about_action.setStatusTip(<span class=\"hljs-string\">'About'<\/span>)\n        about_action.setShortcut(<span class=\"hljs-string\">'F1'<\/span>)\n\n        <span class=\"hljs-comment\"># toolbar<\/span>\n        toolbar = QToolBar(<span class=\"hljs-string\">'Main ToolBar'<\/span>)\n        self.addToolBar(toolbar)\n        toolbar.setIconSize(QSize(<span class=\"hljs-number\">16<\/span>, <span class=\"hljs-number\">16<\/span>))\n\n        toolbar.addAction(new_action)\n        toolbar.addAction(save_action)\n        toolbar.addAction(open_action)\n        toolbar.addSeparator()\n\n        toolbar.addAction(undo_action)\n        toolbar.addAction(redo_action)\n        toolbar.addSeparator()\n\n        toolbar.addAction(exit_action)\n\n        <span class=\"hljs-comment\"># status bar<\/span>\n        self.status_bar = self.statusBar()\n        \n        <span class=\"hljs-comment\"># display the a message in 5 seconds<\/span>\n        self.status_bar.showMessage(<span class=\"hljs-string\">'Ready'<\/span>, <span class=\"hljs-number\">5000<\/span>)\n\n        <span class=\"hljs-comment\"># add a permanent widget to the status bar<\/span>\n        self.character_count = QLabel(<span class=\"hljs-string\">\"Length: 0\"<\/span>)\n        self.status_bar.addPermanentWidget(self.character_count)\n\n        self.show()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_title<\/span><span class=\"hljs-params\">(self, filename=None)<\/span>:<\/span>\n        title = <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{filename <span class=\"hljs-keyword\">if<\/span> filename <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-string\">'Untitled'<\/span>}<\/span> - <span class=\"hljs-subst\">{self.title}<\/span>\"<\/span>\n        self.setWindowTitle(title)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">confirm_save<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> self.text_edit.document().isModified():\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">True<\/span>\n\n        message = <span class=\"hljs-string\">f\"Do you want to save changes to <span class=\"hljs-subst\">{self.path <span class=\"hljs-keyword\">if<\/span> self.path <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-string\">'Untitled'<\/span>}<\/span>?\"<\/span>\n        MsgBoxBtn = QMessageBox.StandardButton\n        MsgBoxBtn = MsgBoxBtn.Save | MsgBoxBtn.Discard | MsgBoxBtn.Cancel\n\n        button = QMessageBox.question(\n            self, self.title, message, buttons=MsgBoxBtn\n        )\n\n        <span class=\"hljs-keyword\">if<\/span> button == MsgBoxBtn.Cancel:\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n        <span class=\"hljs-keyword\">if<\/span> button == MsgBoxBtn.Save:\n            self.save_document()\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">True<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">new_document<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> self.confirm_save():\n            self.text_edit.clear()\n            self.set_title()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">write_file<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.path.write_text(self.text_edit.toPlainText())\n        self.statusBar().showMessage(<span class=\"hljs-string\">'The file has been saved...'<\/span>, <span class=\"hljs-number\">3000<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">save_document<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-comment\"># save the currently openned file<\/span>\n        <span class=\"hljs-keyword\">if<\/span> (self.path):\n            <span class=\"hljs-keyword\">return<\/span> self.write_file()\n\n        <span class=\"hljs-comment\"># save a new file<\/span>\n        filename, _ = QFileDialog.getSaveFileName(\n            self, <span class=\"hljs-string\">'Save File'<\/span>, filter=self.filters\n        )\n\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> filename:\n            <span class=\"hljs-keyword\">return<\/span>\n\n        self.path = Path(filename)\n        self.write_file()\n        self.set_title(filename)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">open_document<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        filename, _ = QFileDialog.getOpenFileName(self, filter=self.filters)\n        <span class=\"hljs-keyword\">if<\/span> filename:\n            self.path = Path(filename)\n            self.text_edit.setText(self.path.read_text())\n            self.set_title(filename)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">quit<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> self.confirm_save():\n            self.destroy()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">text_changed<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        text = self.text_edit.toPlainText()\n        self.character_count.setText(<span class=\"hljs-string\">f'Length: <span class=\"hljs-subst\">{len(text)}<\/span>'<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-comment\"># show the app icon on the taskbar<\/span>\n        <span class=\"hljs-keyword\">import<\/span> ctypes\n        myappid = <span class=\"hljs-string\">'yourcompany.yourproduct.subproduct.version'<\/span>\n        ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)\n    <span class=\"hljs-keyword\">finally<\/span>:\n        app = QApplication(sys.argv)\n        window = MainWindow()\n        sys.exit(app.exec())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works. (We&#8217;ll focus on the status bar part)<\/p>\n\n\n\n<p>First, call the <code>statusBar()<\/code> method to create the status bar for the main window:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.status_bar = self.statusBar()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, show the &#8216;Ready&#8217; message in five seconds using the <code>showMessage()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.status_bar.showMessage(<span class=\"hljs-string\">'Ready'<\/span>, <span class=\"hljs-number\">5000<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, add a permanent widget to the status bar using the <code>addPermanentWidget()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.character_count = QLabel(<span class=\"hljs-string\">\"Length: 0\"<\/span>)\nself.status_bar.addPermanentWidget(self.character_count)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>QLabel<\/code> widget displays the number of characters of the <code>QTextEdit<\/code> widget. By default, it shows zero characters.<\/p>\n\n\n\n<p>After that, connect the <code>textChanged<\/code> signal with the <code>self.text_changed<\/code> slot to update the character count:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.text_edit.textChanged.connect(self.text_changed)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>and you need to define the <code>text_changed<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">text_changed<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        text = self.text_edit.toPlainText()\n        self.character_count.setText(<span class=\"hljs-string\">f'Length: <span class=\"hljs-subst\">{len(text)}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, define the <code>write_file()<\/code> method that saves text to a file and displays a message indicating that the file has been saved for three seconds:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">write_file<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.path.write_text(self.text_edit.toPlainText())\n        self.statusBar().showMessage(<span class=\"hljs-string\">'The file has been saved...'<\/span>, <span class=\"hljs-number\">3000<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>write_file()<\/code> method is called whenever the file is saved.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Qt uses <code>QStatusBar<\/code> class to create a status bar widget.<\/li><li>Use the <code>statusBar()<\/code> method to get a status bar of the main window.<\/li><li>Use the <code>showMessage()<\/code> to display a message on the status bar.<\/li><li>Use the <code>addWidget()<\/code> or <code>addPermanentWidget()<\/code> method to add a widget to the status bar.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5197\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qstatusbar\/\"\n\t\t\t\tdata-post-title=\"PyQt QStatusBar\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5197\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qstatusbar\/\"\n\t\t\t\tdata-post-title=\"PyQt QStatusBar\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PyQt QStatusBar class to create status bar widgets.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":28,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5197","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=5197"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5197\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4862"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=5197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}