{"id":6837,"date":"2023-11-15T08:15:59","date_gmt":"2023-11-15T08:15:59","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=6837"},"modified":"2023-11-15T08:16:01","modified_gmt":"2023-11-15T08:16:01","slug":"tkinter-widget-size","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-widget-size\/","title":{"rendered":"Tkinter Widget Size"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how Tkinter widget size works in various contexts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='understanding-the-tkinter-widget-size'>Understanding the Tkinter widget size <a href=\"#understanding-the-tkinter-widget-size\" class=\"anchor\" id=\"understanding-the-tkinter-widget-size\" title=\"Anchor for Understanding the Tkinter widget size\">#<\/a><\/h2>\n\n\n\n<p>In Tkinter, when you set the width and height for a widget e.g., a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-label\/\">Label<\/a>, the size is measured by characters and lines, not the pixels.<\/p>\n\n\n\n<p>For example, the following program creates a <code>Label<\/code> widget with a width of 8 and position it on the main window:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"446\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-widget-size-demo.png\" alt=\"tkinter widget size demo\" class=\"wp-image-6839\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-widget-size-demo.png 602w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-widget-size-demo-300x222.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p>The width of the label is 8 characters. However, if you increase the font size, the width of the label widget in pixels will also increase accordingly.<\/p>\n\n\n\n<p>For example, the following program sets the width of the <code>Label<\/code> widget to 8 but increases the font size to 24px. As a result, the width of the widget also increases:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\nroot.title(<span class=\"hljs-string\">'Tkinter Widget Size'<\/span>)\r\nroot.geometry(<span class=\"hljs-string\">\"600x400\"<\/span>)\r\n\r\nlabel1 = tk.Label(\r\n    master=root, \r\n    text=<span class=\"hljs-string\">\"Sizing\"<\/span>,\r\n    bg=<span class=\"hljs-string\">'red'<\/span>,\r\n    fg=<span class=\"hljs-string\">'white'<\/span>,\r\n    width=<span class=\"hljs-number\">8<\/span>, \r\n    font=(<span class=\"hljs-string\">'Helvetica'<\/span>,<span class=\"hljs-number\">24<\/span>)\r\n)\r\n\r\nlabel1.pack()\r\n\r\nroot.mainloop()\r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/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=\"602\" height=\"446\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-widget-size-font-size.png\" alt=\"tkinter widget size - font size\" class=\"wp-image-6840\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-widget-size-font-size.png 602w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-widget-size-font-size-300x222.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p>The size of the widget is not only determined by its properties like width and height but also controlled by the layout methods such as <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-pack\/\">pack()<\/a>, <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-grid\/\">grid()<\/a>, and <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-place\/\">place()<\/a>.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\nroot.title(<span class=\"hljs-string\">'Tkinter Widget Size'<\/span>)\r\nroot.geometry(<span class=\"hljs-string\">\"600x400\"<\/span>)\r\n\r\nlabel1 = tk.Label(master=root, text=<span class=\"hljs-string\">\"Sizing\"<\/span>,bg=<span class=\"hljs-string\">'red'<\/span>, fg=<span class=\"hljs-string\">'white'<\/span>, width=<span class=\"hljs-number\">20<\/span>)\r\nlabel1.pack(\r\n    <span class=\"hljs-comment\"># Remove the fill and run the program to see the effect<\/span>\r\n    fill=tk.X \r\n) \r\n\r\nroot.mainloop()\r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/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=\"602\" height=\"446\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-widget-size-pack-fill.png\" alt=\"tkinter widget size - pack fill\" class=\"wp-image-6841\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-widget-size-pack-fill.png 602w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/11\/tkinter-widget-size-pack-fill-300x222.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p>The program sets the width of the <code>Label<\/code> widget to 8 but the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-pack\/\">pack()<\/a> method uses <code>X<\/code> as the value of the fill parameter, which instructs the widget to fill all the horizontal spaces. <\/p>\n\n\n\n<p>As a result, the width of the widget is always the same as the width of the container.<\/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\">\n<li>Set the size of the widget by using the width and height parameters.<\/li>\n\n\n\n<li>The layout methods such as pack(), grid(), and place() may override the size of the widget.<\/li>\n<\/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=\"6837\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-widget-size\/\"\n\t\t\t\tdata-post-title=\"Tkinter Widget Size\"\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=\"6837\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-widget-size\/\"\n\t\t\t\tdata-post-title=\"Tkinter Widget Size\"\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>You will learn how Tkinter widget size works in various contexts including explicitly set the height and width or using layout methods.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":62,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6837","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6837","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=6837"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6837\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=6837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}