{"id":5219,"date":"2022-10-13T07:49:39","date_gmt":"2022-10-13T07:49:39","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5219"},"modified":"2022-10-29T08:07:06","modified_gmt":"2022-10-29T08:07:06","slug":"pyqt-qinputdialog","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qinputdialog\/","title":{"rendered":"PyQt QInputDialog"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PyQt <code>QInputDialog<\/code> class to create an input dialog widget that receives input from the user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qinputdialog-class'>Introduction to the PyQt QInputDialog class <a href=\"#introduction-to-the-pyqt-qinputdialog-class\" class=\"anchor\" id=\"introduction-to-the-pyqt-qinputdialog-class\" title=\"Anchor for Introduction to the PyQt QInputDialog class\">#<\/a><\/h2>\n\n\n\n<p>The <code>QInputDialog<\/code> class creates an input dialog widget that receives the inputs from users. The input value can be a string, an integer, a float, or an item from a list.<\/p>\n\n\n\n<p>The <code>QInputDialog<\/code> has five static methods for getting inputs:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>getText()<\/code> &#8211; allows the user to enter a single string.<\/li><li><code>getInt()<\/code> &#8211; allows the user to enter an integer.<\/li><li><code>getDouble()<\/code> &#8211; allows the user to enter a floating point number.<\/li><li><code>getMultiLineText()<\/code> &#8211; allows the user to enter multiline text.<\/li><li><code>getItem()<\/code> &#8211; allows the user to select an item from a list.<\/li><\/ul>\n\n\n\n<p>These methods return a tuple of two elements. The first element stores the user input and the second element indicate whether the user selects the <code>OK<\/code> or Cancel button.<\/p>\n\n\n\n<p>The following program shows how to display an input dialog that receives a single string from a user:<\/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,  QInputDialog, QWidget, QVBoxLayout,  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 Input Dialog'<\/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 = QVBoxLayout()\n        self.setLayout(layout)\n\n        <span class=\"hljs-comment\"># file selection<\/span>\n        btn = QPushButton(<span class=\"hljs-string\">'Set Window Title'<\/span>)\n        btn.clicked.connect(self.open_input_dialog)\n\n        layout.addWidget(btn)\n\n        self.show()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">open_input_dialog<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        title, ok = QInputDialog.getText(self, <span class=\"hljs-string\">'Title Setting'<\/span>, <span class=\"hljs-string\">'Title:'<\/span>)\n        <span class=\"hljs-keyword\">if<\/span> ok <span class=\"hljs-keyword\">and<\/span> title:\n            self.setWindowTitle(title)\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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"499\" height=\"381\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QInputDialog.png\" alt=\"PyQt QInputDialog\" class=\"wp-image-5221\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QInputDialog.png 499w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QInputDialog-300x229.png 300w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create a button and connect its clicked signal to the <code>open_input_dialog<\/code> method:<\/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\">btn = QPushButton(<span class=\"hljs-string\">'Set Window Title'<\/span>)\nbtn.clicked.connect(self.open_input_dialog)<\/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>Second, define the <code>open_input_dialog<\/code> method and call the <code>getText()<\/code> method to get the title input by the user:<\/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-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">open_input_dialog<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    title, ok = QInputDialog.getText(self, <span class=\"hljs-string\">'Title Setting'<\/span>, <span class=\"hljs-string\">'Title:'<\/span>)\n    <span class=\"hljs-keyword\">if<\/span> ok <span class=\"hljs-keyword\">and<\/span> title:\n        self.setWindowTitle(title)<\/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>getText()<\/code> method returns a tuple of two elements assigned to the title and ok variable<\/p>\n\n\n\n<p>If you enter some text and click the OK button, the <code>title<\/code> will store the input text and the <code>ok<\/code> variable will be True.<\/p>\n\n\n\n<p>However, if you enter some text but click the Cancel button, the <code>title<\/code> variable will store the input text but the <code>ok<\/code> variable is <code>False<\/code> instead.<\/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>Use <code>QInputDialog<\/code> to create an input dialog that receives input from the user.<\/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=\"5219\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qinputdialog\/\"\n\t\t\t\tdata-post-title=\"PyQt QInputDialog\"\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=\"5219\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qinputdialog\/\"\n\t\t\t\tdata-post-title=\"PyQt QInputDialog\"\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 QInputDialog to receive input from the user.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":23,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5219","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5219","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=5219"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5219\/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=5219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}