{"id":5487,"date":"2022-11-09T01:17:54","date_gmt":"2022-11-09T01:17:54","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5487"},"modified":"2022-11-09T01:20:25","modified_gmt":"2022-11-09T01:20:25","slug":"pyqt-qprogressbar","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qprogressbar\/","title":{"rendered":"PyQt QProgressBar"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PyQt <code>QProgressBar<\/code> class to create a progress bar widget.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qprogressbar'>Introduction to the PyQt QProgressBar <a href=\"#introduction-to-the-pyqt-qprogressbar\" class=\"anchor\" id=\"introduction-to-the-pyqt-qprogressbar\" title=\"Anchor for Introduction to the PyQt QProgressBar\">#<\/a><\/h2>\n\n\n\n<p>A progress bar widget notifies the users of the progress of an operation and reassures them that the program is still running.<\/p>\n\n\n\n<p>To create a progress bar widget, you use the <code>QProgressBar<\/code> class:<\/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\">QProgressBar()<\/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>A progress bar has three important values:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The minimum value.<\/li>\n\n\n\n<li>The maximum value.<\/li>\n\n\n\n<li>The current step value.<\/li>\n<\/ul>\n\n\n\n<p>By default, the minimum value defaults to zero, and the maximum value defaults to 100.<\/p>\n\n\n\n<p>To update the progress, you increase the current step value. By doing this, the progress bar will display the percentage of steps that have been completed.<\/p>\n\n\n\n<p>The <code>QProgressBar<\/code> class uses the following formula to calculate the progress of the steps:<\/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\">(current_value - minimum ) \/ (maximum - minimum)<\/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>For example, if the <code>current_value<\/code> is 50, then the percentage of steps is 50%. The progress bar will display the progress like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"54\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/PyQt-QProgressBar-example.png\" alt=\"PyQt QProgressBar example\" class=\"wp-image-5491\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/PyQt-QProgressBar-example.png 432w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/PyQt-QProgressBar-example-300x38.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><\/figure>\n\n\n\n<p>To set the current step value, you use the <code>setValue()<\/code> method. To get the current value, you use the <code>value()<\/code> method. To reset the progress bar so that it shows no progress, you use the <code>reset()<\/code> method.<\/p>\n\n\n\n<p>If you don&#8217;t want to override the default minimum and maximum values, you can use the <code>setMinimum()<\/code> and <code>setMaximum()<\/code> methods to change them. <\/p>\n\n\n\n<p>Also, you can use the <code>setRange()<\/code> method to set both minimum and maximum values at once:<\/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\">setRange(minimum, maximum)<\/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 get the minimum and maximum values, you use the <code>minimum()<\/code> and <code>maximum()<\/code> methods.<\/p>\n\n\n\n<p>In practice, you&#8217;ll use the progress bar widget with <a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qthread\/\">threads<\/a> to update the progress of long-running operations<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='pyqt-qprogressbar-example'>PyQt QProgressBar example <a href=\"#pyqt-qprogressbar-example\" class=\"anchor\" id=\"pyqt-qprogressbar-example\" title=\"Anchor for PyQt QProgressBar example\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>QProgressBar<\/code> class to create a progress bar. <\/p>\n\n\n\n<p>If you click the progress button, it&#8217;ll increase the current value to the maximum value. However, if you click the reset button, it&#8217;ll reset the progress bar.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"452\" height=\"160\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/PyQt-QProgressBar-Program-example.png\" alt=\"PyQt QProgressBar - Program example\" class=\"wp-image-5494\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/PyQt-QProgressBar-Program-example.png 452w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/PyQt-QProgressBar-Program-example-300x106.png 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/figure>\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 shcb-code-table\"><span class='shcb-loc'><span><span class=\"hljs-keyword\">import<\/span> sys\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QProgressBar\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> Qt\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QWidget)<\/span>:<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>    <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<\/span><\/span><span class='shcb-loc'><span>        super().__init__(*args, **kwargs)\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">50<\/span>)\n<\/span><\/span><span class='shcb-loc'><span>        self.setWindowTitle(<span class=\"hljs-string\">'QProgressBar Demo'<\/span>)\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>        layout = QVBoxLayout()\n<\/span><\/span><span class='shcb-loc'><span>        self.setLayout(layout)\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>        hbox = QHBoxLayout()\n<\/span><\/span><mark class='shcb-loc'><span>        self.progress_bar = QProgressBar(self)\n<\/span><\/mark><span class='shcb-loc'><span>        hbox.addWidget(self.progress_bar)\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>        layout.addLayout(hbox)\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>        hbox = QHBoxLayout()\n<\/span><\/span><span class='shcb-loc'><span>        self.btn_progress = QPushButton(<span class=\"hljs-string\">'Progress'<\/span>, clicked=self.progress)\n<\/span><\/span><span class='shcb-loc'><span>        self.bnt_reset = QPushButton(<span class=\"hljs-string\">'Reset'<\/span>, clicked=self.reset)\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-comment\"># align buttons center<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        hbox.addStretch()\n<\/span><\/span><span class='shcb-loc'><span>        hbox.addWidget(self.btn_progress)\n<\/span><\/span><span class='shcb-loc'><span>        hbox.addWidget(self.bnt_reset)\n<\/span><\/span><span class='shcb-loc'><span>        hbox.addStretch()\n<\/span><\/span><span class='shcb-loc'><span>        \n<\/span><\/span><span class='shcb-loc'><span>        layout.addLayout(hbox)\n<\/span><\/span><span class='shcb-loc'><span>        self.current_value = <span class=\"hljs-number\">0<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        self.show()\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">reset<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        self.current_value = <span class=\"hljs-number\">0<\/span>\n<\/span><\/span><mark class='shcb-loc'><span>        self.progress_bar.reset()\n<\/span><\/mark><span class='shcb-loc'><span>\n<\/span><\/span><mark class='shcb-loc'><span>    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">progress<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n<\/span><\/mark><mark class='shcb-loc'><span>        <span class=\"hljs-keyword\">if<\/span> self.current_value  &lt;= self.progress_bar.maximum():\n<\/span><\/mark><mark class='shcb-loc'><span>            self.current_value += <span class=\"hljs-number\">5<\/span>\n<\/span><\/mark><mark class='shcb-loc'><span>            self.progress_bar.setValue(self.current_value)\n<\/span><\/mark><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n<\/span><\/span><span class='shcb-loc'><span>    app = QApplication(sys.argv)\n<\/span><\/span><span class='shcb-loc'><span>    window = MainWindow()\n<\/span><\/span><span class='shcb-loc'><span>    sys.exit(app.exec())\n<\/span><\/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>How it works.<\/p>\n\n\n\n<p>First, create a progress bar using the <code>QProgressBar<\/code> class:<\/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\">self.progress_bar = QProgressBar(self)<\/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>Second, initialize the current value:<\/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.current_value = <span class=\"hljs-number\">0<\/span><\/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>Third, increase the current value by 5 and update it as the current value using the <code>setValue()<\/code> for the progress bar in the <code>progress()<\/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\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">progress<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> self.current_value  &lt;= self.progress_bar.maximum():\n        self.current_value += <span class=\"hljs-number\">5<\/span>\n        self.progress_bar.setValue(self.current_value)<\/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>Finally, reset the current value and call the <code><code>reset()<\/code><\/code> method to reset the progress bar in the <code><code>reset()<\/code><\/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\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">reset<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    self.current_value = <span class=\"hljs-number\">0<\/span>\n    self.progress_bar.reset()<\/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<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\">\n<li>Use the <code>QProgressBar<\/code> class to create progress bar widgets.<\/li>\n\n\n\n<li>Use the <code>setValue()<\/code> to set the current value that reflects the percentage of the current progress.<\/li>\n\n\n\n<li>Use the <code>reset()<\/code> method to reset the progress bar so that it shows no progress.<\/li>\n<\/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=\"5487\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qprogressbar\/\"\n\t\t\t\tdata-post-title=\"PyQt QProgressBar\"\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=\"5487\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qprogressbar\/\"\n\t\t\t\tdata-post-title=\"PyQt QProgressBar\"\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>PyQt QProgressBar<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":21,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5487","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5487","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=5487"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5487\/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=5487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}