{"id":5030,"date":"2022-09-22T03:26:01","date_gmt":"2022-09-22T03:26:01","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5030"},"modified":"2022-09-22T03:26:14","modified_gmt":"2022-09-22T03:26:14","slug":"pyqt-qdatetimeedit","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qdatetimeedit\/","title":{"rendered":"PyQt QDateTimeEdit"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to create a date &amp; time entry widget using the PyQt <code><code>QDateTimeEdit<\/code><\/code> class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qdatetimeedit-widget'>Introduction to the PyQt QDateTimeEdit widget <a href=\"#introduction-to-the-pyqt-qdatetimeedit-widget\" class=\"anchor\" id=\"introduction-to-the-pyqt-qdatetimeedit-widget\" title=\"Anchor for Introduction to the PyQt QDateTimeEdit widget\">#<\/a><\/h2>\n\n\n\n<p>The <code><code>QDateTimeEdit<\/code><\/code> class allows you to create a widget for editing dates &amp; times:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"446\" height=\"144\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QDateTimeEdit.png\" alt=\"PyQt QDateTimeEdit\" class=\"wp-image-5032\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QDateTimeEdit.png 446w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QDateTimeEdit-300x97.png 300w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/figure>\n\n\n\n<p>The <code><code>QDateTimeEdit<\/code><\/code> widget allows you to edit the date and time using the keyboard or up\/down arrow keys to increase\/decrease the value. <\/p>\n\n\n\n<p>Also, you can use the left\/right arrow key to move between the day, month, year, hour, and minute sections of the entry.<\/p>\n\n\n\n<p>The <code><code>QDateTimeEdit<\/code><\/code> has the following useful methods and properties:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Property<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>date()<\/code><\/td><td>Return the date value displayed by the widget. The return type is <code><code><code><code>QDate<\/code><\/code><\/code><\/code>. To convert it to a <code>datetime.date<\/code> object, you use the <code>toPyDate()<\/code> method of the <code><code><code><code>QDate<\/code><\/code><\/code><\/code> class.<\/td><\/tr><tr><td><code>time()<\/code><\/td><td>Return the time displayed by the widget. The return value has the type of&nbsp;<code><code><code>QTime<\/code><\/code><\/code>. Use the <code>toPyTime()<\/code> method to convert it to a Python&nbsp;<code>datetime.time<\/code>&nbsp;object.<\/td><\/tr><tr><td><code>dateTime()<\/code><\/td><td>Return the date and time value displayed by the widget. The return type is <code><code>QDateTime<\/code><\/code>.<\/td><\/tr><tr><td><code>minimumDate<\/code><\/td><td>The earliest date that can be set by the user.<\/td><\/tr><tr><td><code>maximumDate<\/code><\/td><td>The latest date that can be set by the user.<\/td><\/tr><tr><td><code>minimumTime<\/code><\/td><td>The earliest time that can be set by the user.<\/td><\/tr><tr><td><code>maximumTime<\/code><\/td><td>The latest time that can be set by the user.<\/td><\/tr><tr><td><code>minimumDateTime<\/code><\/td><td>The earliest date &amp; time that can be set by the user.<\/td><\/tr><tr><td><code>maximumDateTime<\/code><\/td><td>The latest date &amp; time that can be set by the user.<\/td><\/tr><tr><td><code>calendarPopup<\/code><\/td><td>Display a calendar popup if it is True.<\/td><\/tr><tr><td><code>displayFormat<\/code><\/td><td>is a string that formats the date displayed in the widget.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The <code><code><code><code>QDateTimeEdit<\/code><\/code><\/code><\/code> emits the <code>editingFinished()<\/code> signal when the editing is finished. If you want to trigger an action whenever the value of the <code><code><code><code>QDateTimeEdit<\/code><\/code><\/code><\/code> widget changes, you can connect to the <code>dateTimeChanged()<\/code> signal.<\/p>\n\n\n\n<p>If you want to create a widget for editing dates, you can use <code><a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qdateedit\/\">QDateEdit<\/a><\/code>. Similarly, you can use <code><a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qtimeedit\/\">QTimeEdit<\/a><\/code> to create a widget for editing times only<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='pyqt-qdatetimeedit-widget-example'>PyQt QDateTimeEdit widget example <a href=\"#pyqt-qdatetimeedit-widget-example\" class=\"anchor\" id=\"pyqt-qdatetimeedit-widget-example\" title=\"Anchor for PyQt QDateTimeEdit widget example\">#<\/a><\/h2>\n\n\n\n<p>The following program uses the <code><code>QDateTimeEdit<\/code><\/code> class to create a widget for editing date &amp; time:<\/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, QWidget, QDateTimeEdit, QLabel, QFormLayout\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 QDateTimeEdit'<\/span>)\n        self.setMinimumWidth(<span class=\"hljs-number\">200<\/span>)\n\n        layout = QFormLayout()\n        self.setLayout(layout)\n\n        self.datetime_edit = QDateTimeEdit(self, calendarPopup=<span class=\"hljs-literal\">True<\/span>)\n        self.datetime_edit.dateTimeChanged.connect(self.update)\n\n        self.result_label = QLabel(<span class=\"hljs-string\">''<\/span>, self)\n\n        layout.addRow(<span class=\"hljs-string\">'Date:'<\/span>, self.datetime_edit)\n        layout.addRow(self.result_label)\n\n        self.show()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">update<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        value = self.datetime_edit.dateTime()\n        self.result_label.setText(value.toString(<span class=\"hljs-string\">\"yyyy-MM-dd HH:mm\"<\/span>))\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>First, create the <code><code>QDateTimeEdit<\/code><\/code> widget:<\/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\">self.datetime_edit = QDateTimeEdit(self, calendarPopup=<span class=\"hljs-literal\">True<\/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>Second, connect the <code>dateTimeChanged()<\/code> signal to the <code>update()<\/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\">self.date_edit.dateTimeChanged.connect(self.update)<\/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>Third, create a <code><code>QLabel<\/code><\/code> widget to display the value of the <code><code>date_edit<\/code><\/code> widget once the editing is finished:<\/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\">self.result_label = QLabel(<span class=\"hljs-string\">''<\/span>, self)<\/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>Finally, define the <code>update()<\/code> method that updates the label widget with the current value of the date  &amp; time entry:<\/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-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">update<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    value = self.datetime_edit.dateTime()\n    self.result_label.setText(value.toString(<span class=\"hljs-string\">\"yyyy-MM-dd HH:mm\"<\/span>))<\/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=\"488\" height=\"399\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QDateTimeEdit-Example.png\" alt=\"\" class=\"wp-image-5033\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QDateTimeEdit-Example.png 488w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QDateTimeEdit-Example-300x245.png 300w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/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 the <code><code>QDateTimeEdit<\/code><\/code> to create a date and time entry widget.<\/li><li>Use the <code>dateTime()<\/code> method to get the current value of the <code><code>QDateTimeEdit<\/code><\/code> widget<\/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=\"5030\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qdatetimeedit\/\"\n\t\t\t\tdata-post-title=\"PyQt QDateTimeEdit\"\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=\"5030\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qdatetimeedit\/\"\n\t\t\t\tdata-post-title=\"PyQt QDateTimeEdit\"\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 create a date &#038; time entry widget using the PyQt QDateTimeEdit class.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":15,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5030","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5030","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=5030"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5030\/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=5030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}