{"id":4920,"date":"2022-09-19T10:22:16","date_gmt":"2022-09-19T10:22:16","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4920"},"modified":"2022-10-29T07:43:06","modified_gmt":"2022-10-29T07:43:06","slug":"pyqt-qpushbutton","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qpushbutton\/","title":{"rendered":"PyQt QPushButton"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PyQt <code>QPushButton<\/code> widget to create a push button.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qpushbutton-widget'>Introduction to the PyQt QPushButton widget <a href=\"#introduction-to-the-pyqt-qpushbutton-widget\" class=\"anchor\" id=\"introduction-to-the-pyqt-qpushbutton-widget\" title=\"Anchor for Introduction to the PyQt QPushButton widget\">#<\/a><\/h2>\n\n\n\n<p>The PyQt <code>QPushButton<\/code> class allows you to create a button widget, which can be a push button or a toggle button.<\/p>\n\n\n\n<p>To create a push button, you follow these steps:<\/p>\n\n\n\n<p>First, import <code>QPushButton<\/code> from <code>PyQt6.QtWidgets<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Parser3\" data-shcb-language-slug=\"parser3\"><span><code class=\"hljs language-parser3\"><span class=\"xml\">from PyQt6.QtWidgets import QPushButton<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Parser3<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">parser3<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, call the <code><code>QPushButton<\/code>()<\/code> with a text that appears on the button:<\/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\">button = QPushButton(<span class=\"hljs-string\">'Click Me'<\/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>Third, connect the <code>clicked<\/code> signal to a callable:<\/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\">button.clicked.connect(self.on_clicked)<\/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>The <code>on_clicked<\/code> is a method that executes when the button is clicked. <\/p>\n\n\n\n<p>The following shows the complete program that displays a button on a window:<\/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\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QPushButton, QVBoxLayout\n\n\n<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 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.setWindowTitle(<span class=\"hljs-string\">'PyQt QPushButton Widget'<\/span>)\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">320<\/span>, <span class=\"hljs-number\">210<\/span>)\n\n        button = QPushButton(<span class=\"hljs-string\">'Click Me'<\/span>)\n\n        <span class=\"hljs-comment\"># place the widget on the window<\/span>\n        layout = QVBoxLayout()\n        layout.addWidget(button)\n        self.setLayout(layout)\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n\n    <span class=\"hljs-comment\"># create the main window<\/span>\n    window = MainWindow()\n\n    <span class=\"hljs-comment\"># start the event loop<\/span>\n    sys.exit(app.exec())<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"482\" height=\"361\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QPushButton-Example.png\" alt=\"\" class=\"wp-image-4922\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QPushButton-Example.png 482w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QPushButton-Example-300x225.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-push-button-with-an-icon'>Creating a push button with an icon <a href=\"#creating-a-push-button-with-an-icon\" class=\"anchor\" id=\"creating-a-push-button-with-an-icon\" title=\"Anchor for Creating a push button with an icon\">#<\/a><\/h2>\n\n\n\n<p>To create a button with an icon, you use the following steps:<\/p>\n\n\n\n<p>First, import <code>QIcon<\/code> from <code>PyQt6.QtGui<\/code> module:<\/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\">from<\/span> PyQt6.QtGui <span class=\"hljs-keyword\">import<\/span> QIcon<\/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, create a <code>QPushButton<\/code> object:<\/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\">button = QPushButton(<span class=\"hljs-string\">'Delete'<\/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, add the icon of the button by calling the <code>setIcon()<\/code> method of the <code>QPushButton<\/code> with the <code>QIcon<\/code> object:<\/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\">button.setIcon(QIcon(<span class=\"hljs-string\">'trash.png'<\/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>Note that the <code>QIcon<\/code> object accepts a path to the icon file. To show the <code>trash.png<\/code> icon, you can download it here:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/trash.png\" alt=\"\" class=\"wp-image-4923\"\/><\/figure>\n\n\n\n<p>To make the button nicer, you can set its size by calling <code>setFixedSize()<\/code> method. <\/p>\n\n\n\n<p>The size is determined by the <code>QSize<\/code> object with two arguments width and height. Note that you need to import <code>QSize<\/code> class from <code>PyQt6.QtCore<\/code> module.<\/p>\n\n\n\n<p>The following program shows how to display a button with an icon:<\/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-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QPushButton, QVBoxLayout\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> QSize\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtGui <span class=\"hljs-keyword\">import<\/span> QIcon\n\n\n<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 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.setWindowTitle(<span class=\"hljs-string\">'PyQt QPushButton Widget'<\/span>)\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">320<\/span>, <span class=\"hljs-number\">210<\/span>)\n\n        button = QPushButton(<span class=\"hljs-string\">'Delete'<\/span>)\n        button.setIcon(QIcon(<span class=\"hljs-string\">'trash.png'<\/span>))\n\n        button.setFixedSize(QSize(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">30<\/span>))\n\n        <span class=\"hljs-comment\"># place the widget on the window<\/span>\n        layout = QVBoxLayout()\n        layout.addWidget(button)\n        self.setLayout(layout)\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n\n    <span class=\"hljs-comment\"># create the main window<\/span>\n    window = MainWindow()\n\n    <span class=\"hljs-comment\"># start the event loop<\/span>\n    sys.exit(app.exec())<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"482\" height=\"361\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QPushButton-button-with-icon.png\" alt=\"\" class=\"wp-image-4924\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QPushButton-button-with-icon.png 482w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QPushButton-button-with-icon-300x225.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-toggle-button'>Creating a toggle button <a href=\"#creating-a-toggle-button\" class=\"anchor\" id=\"creating-a-toggle-button\" title=\"Anchor for Creating a toggle button\">#<\/a><\/h2>\n\n\n\n<p>The <code>QPushButton<\/code> class has the <code>checkable<\/code> property that allows you to use the button as a toggle button.<\/p>\n\n\n\n<p>A toggle button has an on\/off state. If the button is on, the checked button is true. Otherwise, it is false.<\/p>\n\n\n\n<p>For a toggle button, the <code>clicked<\/code> signal sends the status of the button, either on or off.<\/p>\n\n\n\n<p>The following program displays a window that has a toggle button:<\/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\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QPushButton, QVBoxLayout\n\n\n<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 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.setWindowTitle(<span class=\"hljs-string\">'PyQt QPushButton Widget'<\/span>)\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">320<\/span>, <span class=\"hljs-number\">210<\/span>)\n\n        button = QPushButton(<span class=\"hljs-string\">'Toggle Me'<\/span>)\n        button.setCheckable(<span class=\"hljs-literal\">True<\/span>)\n        button.clicked.connect(self.on_toggle)\n\n        <span class=\"hljs-comment\"># place the button on the window<\/span>\n        layout = QVBoxLayout()\n        layout.addWidget(button)\n        self.setLayout(layout)\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">on_toggle<\/span><span class=\"hljs-params\">(self, checked)<\/span>:<\/span>\n        print(checked)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n\n    <span class=\"hljs-comment\"># create the main window<\/span>\n    window = MainWindow()\n\n    <span class=\"hljs-comment\"># start the event loop<\/span>\n    sys.exit(app.exec())<\/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<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>Use the PyQt <code>QPushButton<\/code> widget to create a push button or a toggle button.<\/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=\"4920\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qpushbutton\/\"\n\t\t\t\tdata-post-title=\"PyQt QPushButton\"\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=\"4920\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qpushbutton\/\"\n\t\t\t\tdata-post-title=\"PyQt QPushButton\"\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 QPushButton widget to create a button including a push button and a toggle button.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4920","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4920","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=4920"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4920\/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=4920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}