{"id":4965,"date":"2022-09-20T09:12:50","date_gmt":"2022-09-20T09:12:50","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4965"},"modified":"2022-10-14T03:37:45","modified_gmt":"2022-10-14T03:37:45","slug":"pyqt-qgridlayout","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qgridlayout\/","title":{"rendered":"PyQt QGridLayout"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use PyQt <code>QGridLayout<\/code> to arrange widgets in uniform rows and columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qgridlayout'>Introduction to the PyQt QGridLayout <a href=\"#introduction-to-the-pyqt-qgridlayout\" class=\"anchor\" id=\"introduction-to-the-pyqt-qgridlayout\" title=\"Anchor for Introduction to the PyQt QGridLayout\">#<\/a><\/h2>\n\n\n\n<p>The <code>QGridLayout<\/code> allows you to place widgets in uniform <em>rows <\/em>and <em>columns <\/em>of a grid. For example, the following picture shows a grid that consists of four columns and three rows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QGridLayout.svg\" alt=\"PyQt QGridLayout\" class=\"wp-image-4970\"\/><\/figure>\n\n\n\n<p>Rows and columns are <em>zero-based indexing<\/em>. It means that the first row has an index of zero, the second row has an index of one, and so on. Similarly, the first column has an index of zero, the second column has an index of one, etc.<\/p>\n\n\n\n<p>The intersection between a row and a column is called a <em>cell<\/em>. A cell is a space where you can place a widget.<\/p>\n\n\n\n<p>Rows and columns can span. For example, the following picture illustrates that the second column of the first row spans two columns and the second row of the second column spans two rows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QGridLayout-row-and-column-span.svg\" alt=\"PyQt QGridLayout - row and column span\" class=\"wp-image-4972\"\/><\/figure>\n\n\n\n<p>If a widget takes less space than the containing cell, you can align it within the cell using one of the following alignment options:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"435\" height=\"317\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-Grid-AlignmentFlag.gif\" alt=\"\" class=\"wp-image-4976\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Alignment Flag<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>AlignAbsolute<\/code><\/td><td>If the direction is left to right, the <code>AlignLeft<\/code> means align with the <em>right <\/em>edge. However, if you want <code>AlignLeft<\/code> to be always aligned with the left edge, you can combine the <code>AlignLeft<\/code> with the <code>AlignAbsolute<\/code> option.<\/td><\/tr><tr><td><code>AlignBaseline<\/code><\/td><td>Align the widget with the baseline.<\/td><\/tr><tr><td><code>AlignBottom<\/code><\/td><td>Align the widget with the bottom edge.<\/td><\/tr><tr><td><code>AlignCenter<\/code><\/td><td>Centers the widget in both dimensions.<\/td><\/tr><tr><td><code>AlignHCenter<\/code><\/td><td>Centers the widget horizontally in the available space.<\/td><\/tr><tr><td><code>AlignHorizontal_Mask<\/code><\/td><td><code><code>AlignLeft<\/code> | AlignRight | AlignHCenter | AlignJustify | AlignAbsolute<\/code><\/td><\/tr><tr><td><code>AlignJustify<\/code><\/td><td>Justifies the text in the available space.<\/td><\/tr><tr><td><code>AlignLeft<\/code><\/td><td>Align the widget with the left edge.<\/td><\/tr><tr><td><code>AlignRight<\/code><\/td><td>Align the widget with the right edge.<\/td><\/tr><tr><td><code>AlignTop<\/code><\/td><td>Align the widget with the top edge.<\/td><\/tr><tr><td><code>AlignVCenter<\/code><\/td><td>Centers the widget vertically in the available space.<\/td><\/tr><tr><td><code>AlignVertical_Mask<\/code><\/td><td><code><code>AlignTop<\/code> | AlignBottom | AlignVCenter | AlignBaseline<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-a-qgridlayout'>Using a QGridLayout <a href=\"#using-a-qgridlayout\" class=\"anchor\" id=\"using-a-qgridlayout\" title=\"Anchor for Using a QGridLayout\">#<\/a><\/h3>\n\n\n\n<p>To use the <code>QGridLayout<\/code>, you follow these steps:<\/p>\n\n\n\n<p>First, create a <code>QGridLayout<\/code> object:<\/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\">layout = QGridLayout()<\/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>Second, set the parent widget layout:<\/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\">parent.setLayout(layout)<\/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, place the child widgets on the grid layout:<\/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\">layout.addWidget(widget, row, column, rowSpan, columnSpan, alignment)<\/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>In this syntax: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>widget<\/code> is a child widget that you want to place on the grid.<\/li><li><code>row<\/code> is a row index that starts from 0.<\/li><li><code>column<\/code> is a column index that starts from 0.<\/li><li><code>rowSpan<\/code> is the number of rows that you want to span.<\/li><li><code>columnSpan<\/code> is the number of columns that you want to span.<\/li><li><code>alignment<\/code> specifies the alignment of the widget within the cell. <\/li><\/ul>\n\n\n\n<p>To get the alignment value, you import <code>Qt<\/code> from <code>PyQt6.QtCore<\/code>:<\/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\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> Qt<\/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>and use one of the values of the Qt.<code>AlignmentFlag<\/code> enum, for example:<\/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\">Qt.AlignmentFlag.AlignRight<\/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<h2 class=\"wp-block-heading\" id='pyqt-qgridlayout-example'>PyQt QGridLayout example <a href=\"#pyqt-qgridlayout-example\" class=\"anchor\" id=\"pyqt-qgridlayout-example\" title=\"Anchor for PyQt QGridLayout example\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to create a login form using the <code>QGridLayout<\/code>:<\/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\"><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, QLineEdit, QLabel, QGridLayout\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> Qt\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\">'Login Form'<\/span>)\n\n        <span class=\"hljs-comment\"># set the grid layout<\/span>\n        layout = QGridLayout()\n        self.setLayout(layout)\n\n        <span class=\"hljs-comment\"># username<\/span>\n        layout.addWidget(QLabel(<span class=\"hljs-string\">'Username:'<\/span>), <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>)\n        layout.addWidget(QLineEdit(), <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>)\n\n        <span class=\"hljs-comment\"># password<\/span>\n        layout.addWidget(QLabel(<span class=\"hljs-string\">'Password:'<\/span>), <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">0<\/span>)\n        layout.addWidget(QLineEdit(echoMode=QLineEdit.EchoMode.Password), <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>)\n\n        <span class=\"hljs-comment\"># buttons<\/span>\n        layout.addWidget(QPushButton(<span class=\"hljs-string\">'Log in'<\/span>), <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">0<\/span>,\n                         alignment=Qt.AlignmentFlag.AlignRight)\n        layout.addWidget(QPushButton(<span class=\"hljs-string\">'Close'<\/span>), <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>,\n                         alignment=Qt.AlignmentFlag.AlignRight)\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    window = MainWindow()\n    sys.exit(app.exec())\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<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"355\" height=\"196\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QGridLayout-Example.png\" alt=\"PyQt QGridLayout Example\" class=\"wp-image-4981\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QGridLayout-Example.png 355w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QGridLayout-Example-300x166.png 300w\" sizes=\"auto, (max-width: 355px) 100vw, 355px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create a <code>QGridLayout<\/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\">layout = QGridLayout()\nself.setLayout(layout)<\/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>Second, place a <code>QLabel<\/code> on the cell (0,0) and <code><a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qlineedit\/\">QLineEdit<\/a><\/code> on the cell (0,1) for creating the <code>username<\/code> field:<\/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\">layout.addWidget(QLabel(<span class=\"hljs-string\">'Username:'<\/span>), <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>)\nlayout.addWidget(QLineEdit(), <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">1<\/span>)<\/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>Third, place a <code>QLabel<\/code> on the cell (1,0) and <code><a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qlineedit\/\">QLineEdit<\/a><\/code> on the cell (1,1) for creating the <code>password<\/code> field:<\/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\">layout.addWidget(QLabel(<span class=\"hljs-string\">'Password:'<\/span>), <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">0<\/span>)\nlayout.addWidget(QLineEdit(echoMode=QLineEdit.EchoMode.Password), <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>)<\/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<p>Finally, place a login button on the cell (2,0) and align it to the left edge. Also, place the close button on the cell (2,1) and align it to the right edge.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># buttons<\/span>\nlayout.addWidget(QPushButton(<span class=\"hljs-string\">'Log in'<\/span>), <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">0<\/span>,\n                 alignment=Qt.AlignmentFlag.AlignRight)\nlayout.addWidget(QPushButton(<span class=\"hljs-string\">'Close'<\/span>), <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>,\n                 alignment=Qt.AlignmentFlag.AlignRight)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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 PyQt <code>QGridLayout<\/code> to arrange widgets in rows and columns.<\/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=\"4965\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qgridlayout\/\"\n\t\t\t\tdata-post-title=\"PyQt QGridLayout\"\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=\"4965\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qgridlayout\/\"\n\t\t\t\tdata-post-title=\"PyQt QGridLayout\"\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 will learn how to use PyQt QGridLayout to arrange widgets in uniform rows and columns.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4965","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4965","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=4965"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4965\/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=4965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}