{"id":1546,"date":"2020-12-09T03:58:14","date_gmt":"2020-12-09T03:58:14","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1546"},"modified":"2025-04-04T08:45:17","modified_gmt":"2025-04-04T08:45:17","slug":"tkinter-treeview","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-treeview\/","title":{"rendered":"Tkinter Treeview"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Tkinter Treeview widget and how to use it to display  hierarchical and tabular data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introduction-to-the-tkinter-treeview-widget\" id='introduction-to-the-tkinter-treeview-widget'>Introduction to the Tkinter Treeview widget <a href=\"#introduction-to-the-tkinter-treeview-widget\" class=\"anchor\" id=\"introduction-to-the-tkinter-treeview-widget\" title=\"Anchor for Introduction to the Tkinter Treeview widget\">#<\/a><\/h2>\n\n\n\n<p>A <code>Treeview<\/code> widget allows you to display hierarchical collection of items. Each item has a label, an optional <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-photoimage\/\">icon<\/a>, and an optional list of data values. A <code>Treeview<\/code> widget displays data values in successive columns after the tree label.<\/p>\n\n\n\n<p>For example, the following picture shows how to use a <code>Treeview<\/code> to display employees by cities:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"282\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview.png\" alt=\"\" class=\"wp-image-7483\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview.png 645w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-300x131.png 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/figure>\n\n\n\n<p>Additionally, you can use a <code>Treeview<\/code> widget to display the same employee list as a table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"671\" height=\"259\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-table.png\" alt=\"tkinter treeview - table\" class=\"wp-image-7492\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-table.png 671w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-table-300x116.png 300w\" sizes=\"auto, (max-width: 671px) 100vw, 671px\" \/><\/figure>\n\n\n\n<p>The Treeview widget allows both horizontal and vertical <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-scrollbar\/\">scrollbars<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-a-treeview\" id='creating-a-treeview'>Creating a Treeview <a href=\"#creating-a-treeview\" class=\"anchor\" id=\"creating-a-treeview\" title=\"Anchor for Creating a Treeview\">#<\/a><\/h2>\n\n\n\n<p>To create a <code>Treeview<\/code> widget, you follow these steps:<\/p>\n\n\n\n<p>First, import <code>ttk<\/code> module from <code>tkinter<\/code>:<\/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\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk<\/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, create a tree view using the <code>Treeview<\/code> constructor:<\/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\">treeview = ttk.Treeview(master=<span class=\"hljs-literal\">None<\/span>, **kw)<\/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>For example, the following program creates an empty <code>Treeview<\/code>:<\/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-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\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">\"Tkinter Treeview\"<\/span>)\nroot.geometry(<span class=\"hljs-string\">\"500x300\"<\/span>)\n\ntreeview = ttk.Treeview()\n\ntreeview.pack(padx=<span class=\"hljs-number\">10<\/span>,pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-literal\">True<\/span>, fill=tk.BOTH)\n\nroot.mainloop()<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"506\" height=\"336\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-empty.png\" alt=\"tkinter treeview empty\" class=\"wp-image-7484\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-empty.png 506w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-empty-300x199.png 300w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-items-to-a-treeview\" id='adding-items-to-a-treeview'>Adding items to a Treeview <a href=\"#adding-items-to-a-treeview\" class=\"anchor\" id=\"adding-items-to-a-treeview\" title=\"Anchor for Adding items to a Treeview\">#<\/a><\/h2>\n\n\n\n<p>You can add an item to a tree view by using the <code>insert()<\/code> method of the <code>Treeview<\/code> object. For example, the following program adds an item (<code>San Jose<\/code>) to the <code>Treeview<\/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\">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\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">\"400x300\"<\/span>)\nroot.title(<span class=\"hljs-string\">\"Tkinter Treeview\"<\/span>)\n\ntreeview = ttk.Treeview()\nlevel1 = treeview.insert(<span class=\"hljs-string\">\"\"<\/span>, tk.END, text=<span class=\"hljs-string\">\"San Jose\"<\/span>)\n\ntreeview.pack(fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\nroot.mainloop()<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"336\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-one-item.png\" alt=\"tkinter treeview with one item\" class=\"wp-image-7486\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-one-item.png 406w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-one-item-300x248.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/figure>\n\n\n\n<p>In this example, we add an item with label (<code>San Jose<\/code>) to the <code>Treeview<\/code> using the following <code>insert()<\/code> 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\">level1 = treeview.insert(<span class=\"hljs-string\">\"\"<\/span>, tk.END, text=<span class=\"hljs-string\">\"San Jose\"<\/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>The <code>insert()<\/code> method accepts three arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An empty string <code>\"\"<\/code> indicates that the new item is a parent item.<\/li>\n\n\n\n<li><code>tk.END<\/code> instructs the <code>Treeview<\/code>  widget to place the item at the end of the tree.<\/li>\n\n\n\n<li><code>text=\"San Jose\"<\/code> specifies the label of the item.<\/li>\n<\/ul>\n\n\n\n<p>The <code>insert()<\/code> method creates a new item, inserts it into the tree view widget, and returns the item&#8217;s ID (<code>level1<\/code>).<\/p>\n\n\n\n<p>To add a sub item of an item, you also use the <code>insert()<\/code> method:<\/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> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">\"400x300\"<\/span>)\nroot.title(<span class=\"hljs-string\">\"Tkinter Treeview\"<\/span>)\n\ntreeview = ttk.Treeview()\nlevel1 = treeview.insert(<span class=\"hljs-string\">\"\"<\/span>, tk.END, text=<span class=\"hljs-string\">\"San Jose\"<\/span>)\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"John Doe\"<\/span>)\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"Jane Doe\"<\/span>)\n\ntreeview.pack(fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\nroot.mainloop()<\/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=\"506\" height=\"336\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-items.png\" alt=\"tkinter treeview with items\" class=\"wp-image-7485\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-items.png 506w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-items-300x199.png 300w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/figure>\n\n\n\n<p>How it works:<\/p>\n\n\n\n<p>First, insert an item and return the item&#8217;s id:<\/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\">level1 = treeview.insert(<span class=\"hljs-string\">\"\"<\/span>, tk.END, text=<span class=\"hljs-string\">\"San Jose\"<\/span>)<\/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, insert two subitems under the item:<\/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\">treeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"John Doe\"<\/span>)\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"Jane Doe\"<\/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>In this example, we use the <code>level1<\/code> as the id of the parent item.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-columns\" id='creating-columns'>Creating columns <a href=\"#creating-columns\" class=\"anchor\" id=\"creating-columns\" title=\"Anchor for Creating columns\">#<\/a><\/h2>\n\n\n\n<p>An item can have one or more columns. To add the heading for the column, you pass the column names to the <code>Treeview<\/code> constructor:<\/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\"><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\nroot = tk.Tk()\n\nroot.title(<span class=\"hljs-string\">\"Tkinter Treeview\"<\/span>)\n\ntreeview = ttk.Treeview(columns=(<span class=\"hljs-string\">\"Salary\"<\/span>,<span class=\"hljs-string\">\"Bonus\"<\/span>))\n\ntreeview.heading(<span class=\"hljs-string\">\"#0\"<\/span>, text=<span class=\"hljs-string\">\"Employee\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Salary\"<\/span>, text=<span class=\"hljs-string\">\"Salary\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Bonus\"<\/span>, text=<span class=\"hljs-string\">\"Bonus\"<\/span>)\n\nlevel1 = treeview.insert(<span class=\"hljs-string\">''<\/span>, tk.END, text=<span class=\"hljs-string\">\"San Jose\"<\/span>)\n\n\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"John Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">100000<\/span>: ,}<\/span>\"<\/span>,<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">8000<\/span>: ,}<\/span>\"<\/span>))\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"Jane Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">120000<\/span>: ,}<\/span>\"<\/span>,<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">9000<\/span>: ,}<\/span>\"<\/span>))\n\ntreeview.pack(fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\n\nroot.mainloop()<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"628\" height=\"282\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-columns.png\" alt=\"\" class=\"wp-image-7487\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-columns.png 628w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-columns-300x135.png 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create a treeview with two extra columns besides the default one. The default column is <code>'#0'<\/code>.<\/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\">treeview = ttk.Treeview(columns=(<span class=\"hljs-string\">\"Salary\"<\/span>,<span class=\"hljs-string\">\"Bonus\"<\/span>))<\/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<p>Second, set header text for each column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">treeview.heading(<span class=\"hljs-string\">\"#0\"<\/span>, text=<span class=\"hljs-string\">\"Employee\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Salary\"<\/span>, text=<span class=\"hljs-string\">\"Salary\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Bonus\"<\/span>, text=<span class=\"hljs-string\">\"Bonus\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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>'#0'<\/code> refers to the first column. <\/p>\n\n\n\n<p>Third, insert a top-level node:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">level1 = treeview.insert(<span class=\"hljs-string\">''<\/span>, tk.END, text=<span class=\"hljs-string\">\"San Jose\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><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 empty string (<code>''<\/code>) is the root-level item and <code>text=\"San Jose\"<\/code> is the label of the <code>'#0'<\/code> column. The <code>level1<\/code> is the id of the tree node that we&#8217;ll use to insert subitems under it.<\/p>\n\n\n\n<p>Finally, add two subitems as the children of the <code>\"San Jose\"<\/code> node; format the number with comma to make the amounts easier to read:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">treeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"John Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">100000<\/span>: ,}<\/span>\"<\/span>, <span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">8000<\/span>: ,}<\/span>\"<\/span>))\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"Jane Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">120000<\/span>: ,}<\/span>\"<\/span>, <span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">9000<\/span>: ,}<\/span>\"<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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=\"adding-icons\" id='adding-icons'>Adding Icons <a href=\"#adding-icons\" class=\"anchor\" id=\"adding-icons\" title=\"Anchor for Adding Icons\">#<\/a><\/h2>\n\n\n\n<p>To add an icon to an item, you follow these steps:<\/p>\n\n\n\n<p>First, create a&nbsp;<code>PhotoImage<\/code>&nbsp;object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">icon = tk.PhotoImage(file=<span class=\"hljs-string\">'icon.png'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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, pass the icon to the&nbsp;<code>image<\/code>&nbsp;parameter of the&nbsp;<code>insert()<\/code>&nbsp;method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">treeview.insert(..., image=icon)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><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>Before running the following program, you can <a href=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/treeview_assets.zip\" target=\"_blank\" rel=\"noreferrer noopener\">download the icon file<\/a> and extract it to the same directory of the <code>main.py<\/code> file.<\/p>\n\n\n\n<p>The following program shows how to add icons to the items of the <code>Treeview<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" 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\nroot = tk.Tk()\n\nroot.title(<span class=\"hljs-string\">\"Tkinter Treeview\"<\/span>)\n\ntreeview = ttk.Treeview(columns=( <span class=\"hljs-string\">\"Salary\"<\/span>,<span class=\"hljs-string\">\"Bonus\"<\/span>))\n\ntreeview.heading(<span class=\"hljs-string\">\"#0\"<\/span>, text=<span class=\"hljs-string\">\"Employee\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Salary\"<\/span>, text=<span class=\"hljs-string\">\"Salary\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Bonus\"<\/span>, text=<span class=\"hljs-string\">\"Bonus\"<\/span>)\n\n\nicon_city = tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/city.png\"<\/span>)\nicon_male = tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/male.png\"<\/span>)\nicon_female= tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/female.png\"<\/span>)\n\nlevel1 = treeview.insert(<span class=\"hljs-string\">''<\/span>, tk.END, text=<span class=\"hljs-string\">\"San Jose\"<\/span>, image=icon_city)\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"John Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">100000<\/span>: ,}<\/span>\"<\/span>,<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">8000<\/span>: ,}<\/span>\"<\/span>), image=icon_male)\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"Jane Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">120000<\/span>: ,}<\/span>\"<\/span>,<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">9000<\/span>: ,}<\/span>\"<\/span>), image=icon_female)\n\ntreeview.pack(fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><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=\"628\" height=\"282\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-icon.png\" alt=\"tkinter treeview with icon\" class=\"wp-image-7489\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-icon.png 628w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-icon-300x135.png 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create three PhotoImage object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">icon_city = tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/city.png\"<\/span>)\nicon_male = tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/male.png\"<\/span>)\nicon_female= tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/female.png\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><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, assign each of them to the item when inserting into the Treeview widget:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">level1 = treeview.insert(<span class=\"hljs-string\">''<\/span>, tk.END, text=<span class=\"hljs-string\">\"San Jose\"<\/span>, image=icon_city)\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"John Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">100000<\/span>: ,}<\/span>\"<\/span>,<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">8000<\/span>: ,}<\/span>\"<\/span>), image=icon_male)\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"Jane Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">120000<\/span>: ,}<\/span>\"<\/span>,<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">9000<\/span>: ,}<\/span>\"<\/span>), image=icon_female)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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=\"adding-a-scrollbar-to-the-treeview\" id='adding-a-scrollbar-to-the-treeview-widget'>Adding a scrollbar to the Treeview widget <a href=\"#adding-a-scrollbar-to-the-treeview-widget\" class=\"anchor\" id=\"adding-a-scrollbar-to-the-treeview-widget\" title=\"Anchor for Adding a scrollbar to the Treeview widget\">#<\/a><\/h2>\n\n\n\n<p>The following program shows how to add a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-scrollbar\/\">scrollbar<\/a> to a tree view:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" 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\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">\"Tkinter Treeview\"<\/span>)\n\nframe = ttk.Frame(root)\n\n<span class=\"hljs-comment\"># create a treeview widget<\/span>\ntreeview = ttk.Treeview(frame, columns=( <span class=\"hljs-string\">\"Salary\"<\/span>,<span class=\"hljs-string\">\"Bonus\"<\/span>))\n\ntreeview.heading(<span class=\"hljs-string\">\"#0\"<\/span>, text=<span class=\"hljs-string\">\"Employee\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Salary\"<\/span>, text=<span class=\"hljs-string\">\"Salary\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Bonus\"<\/span>, text=<span class=\"hljs-string\">\"Bonus\"<\/span>)\n\n\nicon_city = tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/city.png\"<\/span>)\nlevel1 = treeview.insert(<span class=\"hljs-string\">''<\/span>, tk.END, text=<span class=\"hljs-string\">\"San Jose\"<\/span>, image=icon_city)\n\n\nicon_male = tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/male.png\"<\/span>)\nicon_female= tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/female.png\"<\/span>)\n\n\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"John Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">100000<\/span>: ,}<\/span>\"<\/span>,<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">8000<\/span>: ,}<\/span>\"<\/span>), image=icon_male)\ntreeview.insert(level1, tk.END, text=<span class=\"hljs-string\">\"Jane Doe\"<\/span>, values=(<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">120000<\/span>: ,}<\/span>\"<\/span>,<span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{<span class=\"hljs-number\">9000<\/span>: ,}<\/span>\"<\/span>), image=icon_female)\n\n<span class=\"hljs-comment\"># create a vertical scrollbar<\/span>\nv_scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=treeview.yview)\ntreeview.configure(yscrollcommand=v_scrollbar.set)\n\n<span class=\"hljs-comment\"># pack the treeview and scrollbar<\/span>\ntreeview.pack(side=tk.LEFT, fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>)\nv_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)\n\n<span class=\"hljs-comment\"># package the frame<\/span>\nframe.pack(fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\n\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><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=\"645\" height=\"282\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-a-scrollbar.png\" alt=\"tkinter treeview with a scrollbar\" class=\"wp-image-7490\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-a-scrollbar.png 645w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-with-a-scrollbar-300x131.png 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/figure>\n\n\n\n<p>How it works:<\/p>\n\n\n\n<p>First, create a vertial scrollback and link it with the <code>TreeView<\/code> widget:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">v_scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=treeview.yview)\ntreeview.configure(yscrollcommand=v_scrollbar.set)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><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, pack the scrollback on the same frame of the <code>TreeView<\/code> widget:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">v_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><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=\"loading-data-from-csv-file\" id='loading-data-from-csv-file-and-display-it-on-a-treeview-widget'>Loading data from CSV file and display it on a Treeview widget <a href=\"#loading-data-from-csv-file-and-display-it-on-a-treeview-widget\" class=\"anchor\" id=\"loading-data-from-csv-file-and-display-it-on-a-treeview-widget\" title=\"Anchor for Loading data from CSV file and display it on a Treeview widget\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to read data from a csv file (<code><a href=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/employees.csv\">employee.csv<\/a><\/code>) and show it on a <code>Treeview<\/code> widget:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" 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<span class=\"hljs-keyword\">import<\/span> csv\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">read_csv<\/span><span class=\"hljs-params\">(filename)<\/span>:<\/span>\n    rows = &#91;]\n    <span class=\"hljs-keyword\">with<\/span> open(filename, newline=<span class=\"hljs-string\">''<\/span>) <span class=\"hljs-keyword\">as<\/span> file:\n        reader = csv.DictReader(file)  \n        <span class=\"hljs-keyword\">for<\/span> row <span class=\"hljs-keyword\">in<\/span> reader:\n            rows.append(row) \n\n    <span class=\"hljs-keyword\">return<\/span> rows\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">prepare_tree_data<\/span><span class=\"hljs-params\">(rows, icon_male, icon_female)<\/span>:<\/span>\n    grouped_data = {}\n    <span class=\"hljs-keyword\">for<\/span> row <span class=\"hljs-keyword\">in<\/span> rows:\n        <span class=\"hljs-keyword\">if<\/span> row&#91;<span class=\"hljs-string\">'Gender'<\/span>] == <span class=\"hljs-string\">'Male'<\/span>:\n            row&#91;<span class=\"hljs-string\">'Icon'<\/span>]= icon_male\n        <span class=\"hljs-keyword\">else<\/span>:\n            row&#91;<span class=\"hljs-string\">'Icon'<\/span>]= icon_female\n\n        city = row&#91;<span class=\"hljs-string\">'City'<\/span>]  <span class=\"hljs-comment\"># Assuming city is in the 5th column<\/span>\n        <span class=\"hljs-keyword\">if<\/span> city <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-keyword\">in<\/span> grouped_data:\n            grouped_data&#91;city] = &#91;]\n        grouped_data&#91;city].append(row)\n    <span class=\"hljs-keyword\">return<\/span> grouped_data\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">format_currency<\/span><span class=\"hljs-params\">(value)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{int(value):,}<\/span>\"<\/span>\n    <span class=\"hljs-keyword\">except<\/span> ValueError:\n        <span class=\"hljs-keyword\">return<\/span> value \n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">create_tree_view<\/span><span class=\"hljs-params\">(root, employees, icons)<\/span>:<\/span>\n\n    frame = ttk.Frame(root)\n\n    treeview = ttk.Treeview(frame, columns=( <span class=\"hljs-string\">\"Salary\"<\/span>,<span class=\"hljs-string\">\"Bonus\"<\/span>))\n\n    treeview.heading(<span class=\"hljs-string\">\"#0\"<\/span>, text=<span class=\"hljs-string\">\"Employee\"<\/span>)\n    treeview.heading(<span class=\"hljs-string\">\"Salary\"<\/span>, text=<span class=\"hljs-string\">\"Salary\"<\/span>)\n    treeview.heading(<span class=\"hljs-string\">\"Bonus\"<\/span>, text=<span class=\"hljs-string\">\"Bonus\"<\/span>)\n\n        \n    employee_data = prepare_tree_data(employees, icons&#91;<span class=\"hljs-string\">'female'<\/span>], icons&#91;<span class=\"hljs-string\">'male'<\/span>])\n\n    <span class=\"hljs-keyword\">for<\/span> city <span class=\"hljs-keyword\">in<\/span> employee_data.keys():\n        <span class=\"hljs-comment\"># add city<\/span>\n        city_id = treeview.insert(<span class=\"hljs-string\">''<\/span>, tk.END, text=city, image=icons&#91;<span class=\"hljs-string\">'city'<\/span>])\n\n        <span class=\"hljs-comment\"># add employees in the city<\/span>\n        <span class=\"hljs-keyword\">for<\/span> employee <span class=\"hljs-keyword\">in<\/span> employee_data&#91;city]:\n            treeview.insert(\n                city_id, \n                tk.END, \n                text=employee&#91;<span class=\"hljs-string\">'First Name'<\/span>] + <span class=\"hljs-string\">' '<\/span> + employee&#91;<span class=\"hljs-string\">'Last Name'<\/span>], \n                values=(format_currency(employee&#91;<span class=\"hljs-string\">'Salary'<\/span>]), format_currency(employee&#91;<span class=\"hljs-string\">'Bonus'<\/span>])), \n                image=employee&#91;<span class=\"hljs-string\">'Icon'<\/span>]\n            )\n    <span class=\"hljs-comment\"># create a vertical scrollbar<\/span>\n    v_scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=treeview.yview)\n    treeview.configure(yscrollcommand=v_scrollbar.set)\n\n    <span class=\"hljs-comment\"># pack the treeview and scrollbar<\/span>\n    treeview.pack(side=tk.LEFT, fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>)\n    v_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)\n\n    <span class=\"hljs-comment\"># package the frame<\/span>\n    frame.pack(fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\n\n    \n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    root = tk.Tk()\n    root.title(<span class=\"hljs-string\">\"Employee List\"<\/span>)\n\n    icons = {\n        <span class=\"hljs-string\">'city'<\/span>: tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/city.png\"<\/span>),\n        <span class=\"hljs-string\">'male'<\/span>: tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/male.png\"<\/span>),\n        <span class=\"hljs-string\">'female'<\/span>: tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/female.png\"<\/span>),\n        \n    }\n\n    employees = read_csv(<span class=\"hljs-string\">'employees.csv'<\/span>)\n    create_tree_view(root, employees, icons)    \n                \n    root.mainloop()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    main()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><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=\"645\" height=\"282\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-employee-list.png\" alt=\"tkinter treeview employee list\" class=\"wp-image-7491\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-employee-list.png 645w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-employee-list-300x131.png 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, read data from the a csv file using built-in csv module and return a list of employees:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" 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\">read_csv<\/span><span class=\"hljs-params\">(filename)<\/span>:<\/span>\n    rows = &#91;]\n    <span class=\"hljs-keyword\">with<\/span> open(filename, newline=<span class=\"hljs-string\">''<\/span>) <span class=\"hljs-keyword\">as<\/span> file:\n        reader = csv.DictReader(file)  \n        <span class=\"hljs-keyword\">for<\/span> row <span class=\"hljs-keyword\">in<\/span> reader:\n            rows.append(row) \n\n    <span class=\"hljs-keyword\">return<\/span> rows<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><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, group employees by cities and assign the icon based on employee&#8217;s gender:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" 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\">prepare_tree_data<\/span><span class=\"hljs-params\">(rows, icon_male, icon_female)<\/span>:<\/span>\n    grouped_data = {}\n    <span class=\"hljs-keyword\">for<\/span> row <span class=\"hljs-keyword\">in<\/span> rows:\n        <span class=\"hljs-keyword\">if<\/span> row&#91;<span class=\"hljs-string\">'Gender'<\/span>] == <span class=\"hljs-string\">'Male'<\/span>:\n            row&#91;<span class=\"hljs-string\">'Icon'<\/span>]= icon_male\n        <span class=\"hljs-keyword\">else<\/span>:\n            row&#91;<span class=\"hljs-string\">'Icon'<\/span>]= icon_female\n\n        city = row&#91;<span class=\"hljs-string\">'City'<\/span>]  <span class=\"hljs-comment\"># Assuming city is in the 5th column<\/span>\n        <span class=\"hljs-keyword\">if<\/span> city <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-keyword\">in<\/span> grouped_data:\n            grouped_data&#91;city] = &#91;]\n        grouped_data&#91;city].append(row)\n    <span class=\"hljs-keyword\">return<\/span> grouped_data<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><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, define a function to format currency:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" 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\">format_currency<\/span><span class=\"hljs-params\">(value)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{int(value):,}<\/span>\"<\/span>\n    <span class=\"hljs-keyword\">except<\/span> ValueError:\n        <span class=\"hljs-keyword\">return<\/span> value<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><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>Fourth, create the Treeview widget and add employees to it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" 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\">create_tree_view<\/span><span class=\"hljs-params\">(root, employees, icons)<\/span>:<\/span>\n\n    frame = ttk.Frame(root)\n\n    treeview = ttk.Treeview(frame, columns=( <span class=\"hljs-string\">\"Salary\"<\/span>,<span class=\"hljs-string\">\"Bonus\"<\/span>))\n\n    treeview.heading(<span class=\"hljs-string\">\"#0\"<\/span>, text=<span class=\"hljs-string\">\"Employee\"<\/span>)\n    treeview.heading(<span class=\"hljs-string\">\"Salary\"<\/span>, text=<span class=\"hljs-string\">\"Salary\"<\/span>)\n    treeview.heading(<span class=\"hljs-string\">\"Bonus\"<\/span>, text=<span class=\"hljs-string\">\"Bonus\"<\/span>)\n\n        \n    employee_data = prepare_tree_data(employees, icons&#91;<span class=\"hljs-string\">'female'<\/span>], icons&#91;<span class=\"hljs-string\">'male'<\/span>])\n\n    <span class=\"hljs-keyword\">for<\/span> city <span class=\"hljs-keyword\">in<\/span> employee_data.keys():\n        <span class=\"hljs-comment\"># add city<\/span>\n        city_id = treeview.insert(<span class=\"hljs-string\">''<\/span>, tk.END, text=city, image=icons&#91;<span class=\"hljs-string\">'city'<\/span>])\n\n        <span class=\"hljs-comment\"># add employees in the city<\/span>\n        <span class=\"hljs-keyword\">for<\/span> employee <span class=\"hljs-keyword\">in<\/span> employee_data&#91;city]:\n            treeview.insert(\n                city_id, \n                tk.END, \n                text=employee&#91;<span class=\"hljs-string\">'First Name'<\/span>] + <span class=\"hljs-string\">' '<\/span> + employee&#91;<span class=\"hljs-string\">'Last Name'<\/span>], \n                values=(format_currency(employee&#91;<span class=\"hljs-string\">'Salary'<\/span>]), format_currency(employee&#91;<span class=\"hljs-string\">'Bonus'<\/span>])), \n                image=employee&#91;<span class=\"hljs-string\">'Icon'<\/span>]\n            )\n    <span class=\"hljs-comment\"># create a vertical scrollbar<\/span>\n    v_scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=treeview.yview)\n    treeview.configure(yscrollcommand=v_scrollbar.set)\n\n    <span class=\"hljs-comment\"># pack the treeview and scrollbar<\/span>\n    treeview.pack(side=tk.LEFT, fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>)\n    v_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)\n\n    <span class=\"hljs-comment\"># package the frame<\/span>\n    frame.pack(fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><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>Fifth, define a main() function to create a main window and place the Treeview widget on it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" 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\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    root = tk.Tk()\n    root.title(<span class=\"hljs-string\">\"Employee List\"<\/span>)\n\n    icons = {\n        <span class=\"hljs-string\">'city'<\/span>: tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/city.png\"<\/span>),\n        <span class=\"hljs-string\">'male'<\/span>: tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/male.png\"<\/span>),\n        <span class=\"hljs-string\">'female'<\/span>: tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/female.png\"<\/span>),\n        \n    }\n\n    employees = read_csv(<span class=\"hljs-string\">'employees.csv'<\/span>)\n    create_tree_view(root, employees, icons)    \n                \n    root.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><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, call the main() function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    main()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><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='displaying-tabular-data'>Displaying tabular data <a href=\"#displaying-tabular-data\" class=\"anchor\" id=\"displaying-tabular-data\" title=\"Anchor for Displaying tabular data\">#<\/a><\/h2>\n\n\n\n<p>The following program shows how to use a <code>Treeview<\/code> widget to display tabular data:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" 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<span class=\"hljs-keyword\">import<\/span> csv\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">read_csv<\/span><span class=\"hljs-params\">(filename)<\/span>:<\/span>\n    rows = &#91;]\n    <span class=\"hljs-keyword\">with<\/span> open(filename, newline=<span class=\"hljs-string\">''<\/span>) <span class=\"hljs-keyword\">as<\/span> file:\n        reader = csv.DictReader(file)\n        <span class=\"hljs-keyword\">for<\/span> row <span class=\"hljs-keyword\">in<\/span> reader:\n            rows.append(row)\n    <span class=\"hljs-keyword\">return<\/span> rows\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">format_currency<\/span><span class=\"hljs-params\">(value)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"$<span class=\"hljs-subst\">{int(value):,}<\/span>\"<\/span>\n    <span class=\"hljs-keyword\">except<\/span> ValueError:\n        <span class=\"hljs-keyword\">return<\/span> value\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">create_table_view<\/span><span class=\"hljs-params\">(root, employees)<\/span>:<\/span>\n    frame = ttk.Frame(root)\n\n    <span class=\"hljs-comment\"># Define the Treeview with columns<\/span>\n    treeview = ttk.Treeview(\n        frame, \n        columns=(\n            <span class=\"hljs-string\">\"First Name\"<\/span>, \n            <span class=\"hljs-string\">\"Last Name\"<\/span>,  \n            <span class=\"hljs-string\">\"Gender\"<\/span>,\n            <span class=\"hljs-string\">\"City\"<\/span>, \n            <span class=\"hljs-string\">\"Salary\"<\/span>, \n            <span class=\"hljs-string\">\"Bonus\"<\/span>\n        ), \n        show=<span class=\"hljs-string\">\"headings\"<\/span>\n    )\n\n    <span class=\"hljs-comment\"># Define column headings<\/span>\n    treeview.heading(<span class=\"hljs-string\">\"First Name\"<\/span>, text=<span class=\"hljs-string\">\"First Name\"<\/span>)\n    treeview.heading(<span class=\"hljs-string\">\"Last Name\"<\/span>, text=<span class=\"hljs-string\">\"Last Name\"<\/span>)\n    treeview.heading(<span class=\"hljs-string\">\"Gender\"<\/span>, text=<span class=\"hljs-string\">\"Gender\"<\/span>)\n    treeview.heading(<span class=\"hljs-string\">\"City\"<\/span>, text=<span class=\"hljs-string\">\"City\"<\/span>)\n    treeview.heading(<span class=\"hljs-string\">\"Salary\"<\/span>, text=<span class=\"hljs-string\">\"Salary\"<\/span>)\n    treeview.heading(<span class=\"hljs-string\">\"Bonus\"<\/span>, text=<span class=\"hljs-string\">\"Bonus\"<\/span>)\n    \n\n    <span class=\"hljs-comment\"># Define column widths<\/span>\n    treeview.column(<span class=\"hljs-string\">\"First Name\"<\/span>, width=<span class=\"hljs-number\">150<\/span>)\n    treeview.column(<span class=\"hljs-string\">\"Last Name\"<\/span>, width=<span class=\"hljs-number\">150<\/span>)\n    treeview.column(<span class=\"hljs-string\">\"Gender\"<\/span>, width=<span class=\"hljs-number\">100<\/span>)\n    treeview.column(<span class=\"hljs-string\">\"City\"<\/span>, width=<span class=\"hljs-number\">150<\/span>)\n    treeview.column(<span class=\"hljs-string\">\"Salary\"<\/span>, width=<span class=\"hljs-number\">100<\/span>, anchor=tk.E)\n    treeview.column(<span class=\"hljs-string\">\"Bonus\"<\/span>, width=<span class=\"hljs-number\">100<\/span>, anchor=tk.E)\n    \n\n    <span class=\"hljs-comment\"># Populate the table with employee data<\/span>\n    <span class=\"hljs-keyword\">for<\/span> employee <span class=\"hljs-keyword\">in<\/span> employees:\n        treeview.insert(\n            <span class=\"hljs-string\">\"\"<\/span>,\n            tk.END,\n            values=(\n                employee&#91;<span class=\"hljs-string\">\"First Name\"<\/span>],\n                employee&#91;<span class=\"hljs-string\">\"Last Name\"<\/span>],\n                employee&#91;<span class=\"hljs-string\">\"Gender\"<\/span>],\n                employee&#91;<span class=\"hljs-string\">\"City\"<\/span>],\n                format_currency(employee&#91;<span class=\"hljs-string\">\"Salary\"<\/span>]),\n                format_currency(employee&#91;<span class=\"hljs-string\">\"Bonus\"<\/span>]),\n                \n            ),\n        )\n\n    <span class=\"hljs-comment\"># Create a vertical scrollbar<\/span>\n    v_scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=treeview.yview)\n    treeview.configure(yscrollcommand=v_scrollbar.set)\n\n    <span class=\"hljs-comment\"># Pack the Treeview and scrollbar<\/span>\n    treeview.pack(side=tk.LEFT, fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>)\n    v_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)\n\n    <span class=\"hljs-comment\"># Pack the frame<\/span>\n    frame.pack(fill=tk.BOTH, expand=<span class=\"hljs-literal\">True<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    root = tk.Tk()\n    root.title(<span class=\"hljs-string\">\"Employee List\"<\/span>)\n\n    employees = read_csv(<span class=\"hljs-string\">'employees.csv'<\/span>)\n    create_table_view(root, employees)\n\n    root.mainloop()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    main()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><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=\"793\" height=\"280\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-table-display.png\" alt=\"\" class=\"wp-image-7493\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-table-display.png 793w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-table-display-300x106.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-treeview-table-display-768x271.png 768w\" sizes=\"auto, (max-width: 793px) 100vw, 793px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create a <code>Treeview<\/code> widget without the default tree column (<code>#0<\/code>) and displays column headings by specifying <code>show=\"headings\"<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">treeview = ttk.Treeview(\n        frame, \n        columns=(\n            <span class=\"hljs-string\">\"First Name\"<\/span>, \n            <span class=\"hljs-string\">\"Last Name\"<\/span>,  \n            <span class=\"hljs-string\">\"Gender\"<\/span>,\n            <span class=\"hljs-string\">\"City\"<\/span>, \n            <span class=\"hljs-string\">\"Salary\"<\/span>, \n            <span class=\"hljs-string\">\"Bonus\"<\/span>\n        ), \n        show=<span class=\"hljs-string\">\"headings\"<\/span>\n    )<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><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>This code makes the <code>Treeview<\/code> widget look like a table (with rows and columns), instead of a hierarchical tree.<\/p>\n\n\n\n<p>Second, define a heading for each column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">treeview.heading(<span class=\"hljs-string\">\"First Name\"<\/span>, text=<span class=\"hljs-string\">\"First Name\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Last Name\"<\/span>, text=<span class=\"hljs-string\">\"Last Name\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Gender\"<\/span>, text=<span class=\"hljs-string\">\"Gender\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"City\"<\/span>, text=<span class=\"hljs-string\">\"City\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Salary\"<\/span>, text=<span class=\"hljs-string\">\"Salary\"<\/span>)\ntreeview.heading(<span class=\"hljs-string\">\"Bonus\"<\/span>, text=<span class=\"hljs-string\">\"Bonus\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><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, define column widths and align the data on the right using the <code>anchor<\/code> <code>tk.E<\/code> (<code>E<\/code> stands for <code>East<\/code>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">treeview.column(<span class=\"hljs-string\">\"First Name\"<\/span>, width=<span class=\"hljs-number\">150<\/span>)\ntreeview.column(<span class=\"hljs-string\">\"Last Name\"<\/span>, width=<span class=\"hljs-number\">150<\/span>)\ntreeview.column(<span class=\"hljs-string\">\"Gender\"<\/span>, width=<span class=\"hljs-number\">100<\/span>)\ntreeview.column(<span class=\"hljs-string\">\"City\"<\/span>, width=<span class=\"hljs-number\">150<\/span>)\ntreeview.column(<span class=\"hljs-string\">\"Salary\"<\/span>, width=<span class=\"hljs-number\">100<\/span>, anchor=tk.E)\ntreeview.column(<span class=\"hljs-string\">\"Bonus\"<\/span>, width=<span class=\"hljs-number\">100<\/span>, anchor=tk.E)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><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 a Tkinter <code>Treeview<\/code> widget to display both tabular and hierarchical data.<\/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=\"1546\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-treeview\/\"\n\t\t\t\tdata-post-title=\"Tkinter Treeview\"\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=\"1546\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-treeview\/\"\n\t\t\t\tdata-post-title=\"Tkinter Treeview\"\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 about the Tkinter Treeview widget and how to use it to display both tabular and hierarchical data.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":28,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1546","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1546","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=1546"}],"version-history":[{"count":5,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1546\/revisions"}],"predecessor-version":[{"id":7507,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1546\/revisions\/7507"}],"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=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}