{"id":5224,"date":"2022-10-13T09:11:06","date_gmt":"2022-10-13T09:11:06","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5224"},"modified":"2022-10-29T08:05:33","modified_gmt":"2022-10-29T08:05:33","slug":"pyqt-qmessagebox","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qmessagebox\/","title":{"rendered":"PyQt QMessageBox"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PyQt <code>QMessageBox<\/code> class to create a modal dialog that alerts the user or asks the user to make a decision.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-pyqt-qmessagebox-class'>Introduction to PyQt QMessageBox class <a href=\"#introduction-to-pyqt-qmessagebox-class\" class=\"anchor\" id=\"introduction-to-pyqt-qmessagebox-class\" title=\"Anchor for Introduction to PyQt QMessageBox class\">#<\/a><\/h2>\n\n\n\n<p>The <code>QMessageBox<\/code> class allows you to create a modal dialog that alerts the user with important information or asks the user a question and receives an answer.<\/p>\n\n\n\n<p>The <code>QMessageBox<\/code> provides some useful static methods for displaying a message box:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>information()<\/code> &#8211; show an information message.<\/li><li><code>question()<\/code> &#8211; ask the user a question and receives an answer.<\/li><li><code>warning()<\/code> &#8211; show a warning message.<\/li><li><code>critical()<\/code> &#8211; display critical information.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='pyqt-qmessagebox-examples'>PyQt QMessageBox examples <a href=\"#pyqt-qmessagebox-examples\" class=\"anchor\" id=\"pyqt-qmessagebox-examples\" title=\"Anchor for PyQt QMessageBox examples\">#<\/a><\/h2>\n\n\n\n<p>The following program shows a window with four buttons, clicking a button will display a corresponding message:<\/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\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication,  QMessageBox, QWidget, QHBoxLayout,  QPushButton\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 QMessageBox'<\/span>)\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">100<\/span>)\n\n        layout = QHBoxLayout()\n        self.setLayout(layout)\n\n        btn_question = QPushButton(<span class=\"hljs-string\">'Question'<\/span>)\n        btn_question.clicked.connect(self.question)\n\n        btn_info = QPushButton(<span class=\"hljs-string\">'Information'<\/span>)\n        btn_info.clicked.connect(self.info)\n\n        btn_warning = QPushButton(<span class=\"hljs-string\">'Warning'<\/span>)\n        btn_warning.clicked.connect(self.warning)\n\n        btn_critical = QPushButton(<span class=\"hljs-string\">'Critical'<\/span>)\n        btn_critical.clicked.connect(self.critical)\n\n        layout.addWidget(btn_question)\n        layout.addWidget(btn_info)\n        layout.addWidget(btn_warning)\n        layout.addWidget(btn_critical)\n\n        self.show()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">info<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        QMessageBox.information(\n            self,\n            <span class=\"hljs-string\">'Information'<\/span>,\n            <span class=\"hljs-string\">'This is important information.'<\/span>\n        )\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">warning<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        QMessageBox.warning(\n            self,\n            <span class=\"hljs-string\">'Warning'<\/span>,\n            <span class=\"hljs-string\">'This is a warning message.'<\/span>\n        )\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">critical<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        QMessageBox.critical(\n            self,\n            <span class=\"hljs-string\">'Critical'<\/span>,\n            <span class=\"hljs-string\">'This is a critical message.'<\/span>\n        )\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">question<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        answer = QMessageBox.question(\n            self,\n            <span class=\"hljs-string\">'Confirmation'<\/span>,\n            <span class=\"hljs-string\">'Do you want to quit?'<\/span>,\n            QMessageBox.StandardButton.Yes |\n            QMessageBox.StandardButton.No\n        )\n        <span class=\"hljs-keyword\">if<\/span> answer == QMessageBox.StandardButton.Yes:\n            QMessageBox.information(\n                self,\n                <span class=\"hljs-string\">'Information'<\/span>,\n                <span class=\"hljs-string\">'You selected Yes. The program will be terminated.'<\/span>,\n                QMessageBox.StandardButton.Ok\n            )\n            self.close()\n        <span class=\"hljs-keyword\">else<\/span>:\n            QMessageBox.information(\n                self,\n                <span class=\"hljs-string\">'Information'<\/span>,\n                <span class=\"hljs-string\">'You selected No.'<\/span>,\n                QMessageBox.StandardButton.Ok\n            )\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    sys.exit(app.exec())<\/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>How it works.<\/p>\n\n\n\n<p>The <code>question()<\/code> method displays a message box with a question that asks the user to select either a Yes or No 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\">answer = QMessageBox.question(\n    self,\n    <span class=\"hljs-string\">'Confirmation'<\/span>,\n    <span class=\"hljs-string\">'Do you want to quit?'<\/span>,\n    QMessageBox.StandardButton.Yes |\n    QMessageBox.StandardButton.No\n)<\/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<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"367\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Question-Message.png\" alt=\"\" class=\"wp-image-5228\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Question-Message.png 544w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Question-Message-300x202.png 300w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/figure>\n\n\n\n<p>To get which button the user clicked, you compare the return value of the <code>question()<\/code> method with the <code>Yes<\/code> and <code>No<\/code> members of the <code>QMessageBox.StandardButton<\/code> enum:<\/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\"><span class=\"hljs-keyword\">if<\/span> answer == QMessageBox.StandardButton.Yes:\n    QMessageBox.information(\n        self,\n        <span class=\"hljs-string\">'Information'<\/span>,\n        <span class=\"hljs-string\">'You selected Yes. The program will be terminated.'<\/span>,\n        QMessageBox.StandardButton.Ok\n    )\n    self.close()\n<span class=\"hljs-keyword\">else<\/span>:\n    QMessageBox.information(\n        self,\n        <span class=\"hljs-string\">'Information'<\/span>,\n        <span class=\"hljs-string\">'You selected No.'<\/span>,\n        QMessageBox.StandardButton.Ok\n    )<\/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>information()<\/code> method displays a message box with information. It accepts the parent widget, the title of the message box, and the message.<\/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\"> QMessageBox.information(\n    self,\n    <span class=\"hljs-string\">'Information'<\/span>,\n    <span class=\"hljs-string\">'This is important information.'<\/span>\n)<\/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<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"550\" height=\"397\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Information-Message.png\" alt=\"\" class=\"wp-image-5227\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Information-Message.png 550w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Information-Message-300x217.png 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><figcaption>PyQt-QMessageBox-Information-Message<\/figcaption><\/figure>\n\n\n\n<p>The <code>warning()<\/code> method displays a warning message. Its appearance is like the information except for the warning icon:<\/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\">QMessageBox.warning(\n    self,\n    <span class=\"hljs-string\">'Warning'<\/span>,\n    <span class=\"hljs-string\">'This is a warning message.'<\/span>\n)<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"381\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Warning-Message.png\" alt=\"\" class=\"wp-image-5226\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Warning-Message.png 557w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Warning-Message-300x205.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/figure>\n\n\n\n<p>The <code>critical()<\/code> method displays a critical message on the message box. The stop icon makes the message critical.<\/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\">QMessageBox.critical(\n    self,\n    <span class=\"hljs-string\">'Critical'<\/span>,\n    <span class=\"hljs-string\">'This is a critical message.'<\/span>\n)<\/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<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"560\" height=\"388\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Critical-Message.png\" alt=\"\" class=\"wp-image-5229\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Critical-Message.png 560w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QMessageBox-Critical-Message-300x208.png 300w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/figure>\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>Use <code>QMessageBox<\/code> class to create a modal dialog that displays a message box or asks the user a question and receives an answer.<\/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=\"5224\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qmessagebox\/\"\n\t\t\t\tdata-post-title=\"PyQt QMessageBox\"\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=\"5224\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qmessagebox\/\"\n\t\t\t\tdata-post-title=\"PyQt QMessageBox\"\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 QMessageBox class to create a modal dialog that alerts the user or asks the user to make a decision.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":22,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5224","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5224","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=5224"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5224\/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=5224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}