{"id":1484,"date":"2020-12-04T02:22:16","date_gmt":"2020-12-04T02:22:16","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1484"},"modified":"2023-11-16T01:31:41","modified_gmt":"2023-11-16T01:31:41","slug":"tkinter-notebook","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-notebook\/","title":{"rendered":"Tkinter Notebook"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Tkinter Notebook widget to create tabs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-notebook-widget'>Introduction to the Tkinter Notebook widget <a href=\"#introduction-to-the-tkinter-notebook-widget\" class=\"anchor\" id=\"introduction-to-the-tkinter-notebook-widget\" title=\"Anchor for Introduction to the Tkinter Notebook widget\">#<\/a><\/h2>\n\n\n\n<p>The <code>Notebook<\/code> widget allows you to select pages of contents by clicking on tabs:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"88\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Notebook-Example.png\" alt=\"\" class=\"wp-image-1487\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Notebook-Example.png 402w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Notebook-Example-300x66.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\n<\/div>\n\n\n<p>When you click one of these tabs, the <code>Notebook<\/code> widget will display a child pane associated with the selected tab. Typically, a child pane is a <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-frame\/\">Frame<\/a><\/code> widget.<\/p>\n\n\n\n<p>To create a <code>Notebook<\/code> widget, you use the <code>ttk.Notebook<\/code> class as follows:<\/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\">notebook = ttk.Notebook(master,**kw)<\/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>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>master<\/code>: This is the parent widget of the notebook. Typically, it&#8217;s the main window.<\/li>\n\n\n\n<li><code>**kw<\/code>: This allows you to specify keyword arguments that control the appearance of the Notebook widget.<\/li>\n<\/ul>\n\n\n\n<p>The Notebook widget has some useful options. For example, you use <code>height<\/code> and <code>width<\/code> options to specify the space allocated to the widget. Also, you can add some space around the outside of the widget by using the <code>padding<\/code> option.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='notebook-methods'>Notebook methods <a href=\"#notebook-methods\" class=\"anchor\" id=\"notebook-methods\" title=\"Anchor for Notebook methods\">#<\/a><\/h2>\n\n\n\n<p>The <code>ttk.Notebook<\/code> class provides you with many handy methods that allow you to manage tabs effectively.<\/p>\n\n\n\n<p>The following describes the most commonly used ones:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='addchild-kwargs'>add(child, **kwargs) <a href=\"#addchild-kwargs\" class=\"anchor\" id=\"addchild-kwargs\" title=\"Anchor for add(child, **kwargs)\">#<\/a><\/h3>\n\n\n\n<p>The <code>add()<\/code> method adds a child widget to a window. The <code>**kwargs<\/code> argument is one or more options. Here are the important ones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>child<\/code> is a widget to add to the notebook. <\/li>\n\n\n\n<li>The <code>text<\/code> option specifies the label that appears on the tab<\/li>\n\n\n\n<li>The <code>image<\/code> option specifies the image to be displayed on the tab.<\/li>\n\n\n\n<li>If you use both <code>text<\/code> and <code>image<\/code> options, you need to use the <code>compound<\/code> option. The compound option describes the position of the image relative to the text. It can be <code>tk.TOP<\/code>, <code>tk.BOTTOM<\/code>, <code>tk.LEFT<\/code>, <code>tk.RIGHT<\/code>, <code>tk.CENTER<\/code>. For example, <code>tk.LEFT<\/code> would place the <code>image<\/code> to the left of the <code>text<\/code>.<\/li>\n\n\n\n<li>The <code>underline<\/code> option that takes zero or a positive integer. It specifies the character at a position of the text on the tab to be underlined.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='hidetabid'>hide(tabId) <a href=\"#hidetabid\" class=\"anchor\" id=\"hidetabid\" title=\"Anchor for hide(tabId)\">#<\/a><\/h3>\n\n\n\n<p>The <code>hide()<\/code> method temporarily removes the tab identified by the <code>tabId<\/code> from the Notebook. Tabs have a zero-based index, meaning that the first tab starts at zero.<\/p>\n\n\n\n<p>To show the tab, you need to call the <code>add()<\/code> method again. There&#8217;s no corresponding <code>show()<\/code> method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='forgetchild'>forget(child) <a href=\"#forgetchild\" class=\"anchor\" id=\"forgetchild\" title=\"Anchor for forget(child)\">#<\/a><\/h3>\n\n\n\n<p>The <code>forget()<\/code> permanently removes the specified <code>child<\/code> widget from the notebook.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-notebook-widget-example'>Tkinter Notebook widget example <a href=\"#tkinter-notebook-widget-example\" class=\"anchor\" id=\"tkinter-notebook-widget-example\" title=\"Anchor for Tkinter Notebook widget example\">#<\/a><\/h2>\n\n\n\n<p>The following program shows how to create a notebook with two tabs:<\/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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n\n<span class=\"hljs-comment\"># root window<\/span>\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">'400x300'<\/span>)\nroot.title(<span class=\"hljs-string\">'Notebook Demo'<\/span>)\n\n<span class=\"hljs-comment\"># create a notebook<\/span>\nnotebook = ttk.Notebook(root)\nnotebook.pack(pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-literal\">True<\/span>)\n\n<span class=\"hljs-comment\"># create frames<\/span>\nframe1 = ttk.Frame(notebook, width=<span class=\"hljs-number\">400<\/span>, height=<span class=\"hljs-number\">280<\/span>)\nframe2 = ttk.Frame(notebook, width=<span class=\"hljs-number\">400<\/span>, height=<span class=\"hljs-number\">280<\/span>)\n\nframe1.pack(fill=<span class=\"hljs-string\">'both'<\/span>, expand=<span class=\"hljs-literal\">True<\/span>)\nframe2.pack(fill=<span class=\"hljs-string\">'both'<\/span>, expand=<span class=\"hljs-literal\">True<\/span>)\n\n<span class=\"hljs-comment\"># add frames to notebook<\/span>\n\nnotebook.add(frame1, text=<span class=\"hljs-string\">'General Information'<\/span>)\nnotebook.add(frame2, text=<span class=\"hljs-string\">'Profile'<\/span>)\n\n\nroot.mainloop()<\/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>Output:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"332\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Notebook.png\" alt=\"\" class=\"wp-image-1485\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Notebook.png 402w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Notebook-300x248.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\n<\/div>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create a notebook widget whose parent is the root window:<\/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\">notebook = ttk.Notebook(root)\nnotebook.pack(pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-literal\">True<\/span>)<\/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>Second, create two frames whose parent is the notebook:<\/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\">frame1 = ttk.Frame(notebook, width=<span class=\"hljs-number\">400<\/span>, height=<span class=\"hljs-number\">280<\/span>)\nframe2 = ttk.Frame(notebook, width=<span class=\"hljs-number\">400<\/span>, height=<span class=\"hljs-number\">280<\/span>)\n\nframe1.pack(fill=<span class=\"hljs-string\">'both'<\/span>, expand=<span class=\"hljs-literal\">True<\/span>)\nframe2.pack(fill=<span class=\"hljs-string\">'both'<\/span>, expand=<span class=\"hljs-literal\">True<\/span>)<\/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>Third, add these frames to the notebook by using the add() method:<\/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\">notebook.add(frame1, text=<span class=\"hljs-string\">'General Information'<\/span>)\nnotebook.add(frame2, text=<span class=\"hljs-string\">'Profile'<\/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<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>Use the <code>ttk.Notebook<\/code> class to create a notebook widget.<\/li>\n\n\n\n<li>Use the <code>add()<\/code> method to add a tab to the notebook.<\/li>\n\n\n\n<li>Use the <code>hide()<\/code> method to temporarily remove a tab from the notebook. To remove a tab permanently, use the <code>forget()<\/code> method.<\/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=\"1484\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-notebook\/\"\n\t\t\t\tdata-post-title=\"Tkinter Notebook\"\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=\"1484\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-notebook\/\"\n\t\t\t\tdata-post-title=\"Tkinter Notebook\"\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 Tkinter Notebook widget to create tabs.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":27,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1484","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1484","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=1484"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1484\/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=1484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}