Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Create Table Using Tkinter

Creating a table in Tkinter can be achieved using multiple methods. One common approach is to use a combination of Label and Frame widgets. Another approach is using the ttk.Treeview widget, which offers table-like features.

In this tutorial, I will guide you through creating a basic table using both methods:

1. Using Label and Frame:

  1. Setting up the basic GUI with Tkinter:

    import tkinter as tk
    
    root = tk.Tk()
    root.title("Table with Label & Frame")
    
  2. Function to create table:

    def create_table(rows, cols, data):
        for i in range(rows):
            for j in range(cols):
                label = tk.Label(root, text=data[i][j], borderwidth=1, relief=tk.SOLID)
                label.grid(row=i, column=j, sticky="nsew")
    
        for i in range(rows):
            root.grid_rowconfigure(i, weight=1)
        for j in range(cols):
            root.grid_columnconfigure(j, weight=1)
    
  3. Create table data and call the function:

    data = [
        ["Name", "Age", "Country"],
        ["Alice", "25", "USA"],
        ["Bob", "30", "UK"],
        ["Charlie", "35", "Canada"]
    ]
    
    create_table(4, 3, data)
    
  4. Run the main loop:

    root.mainloop()
    

2. Using ttk.Treeview:

  1. Setting up the basic GUI with Tkinter and ttk:

    import tkinter as tk
    from tkinter import ttk
    
    root = tk.Tk()
    root.title("Table with ttk.Treeview")
    
  2. Function to create table:

    def create_table_with_treeview(data):
        tree = ttk.Treeview(root, columns=list(data[0].keys()), show="headings")
    
        for col in data[0].keys():
            tree.heading(col, text=col)
            tree.column(col, width=100)
    
        for item in data:
            tree.insert("", "end", values=list(item.values()))
    
        tree.pack(pady=20, padx=20)
    
  3. Create table data and call the function:

    data = [
        {"Name": "Alice", "Age": "25", "Country": "USA"},
        {"Name": "Bob", "Age": "30", "Country": "UK"},
        {"Name": "Charlie", "Age": "35", "Country": "Canada"}
    ]
    
    create_table_with_treeview(data)
    
  4. Run the main loop:

    root.mainloop()
    

The first approach using Label and Frame provides a basic way of displaying data in a tabular format. However, if you need more advanced table features like columns sorting, better interactivity, and scalability for larger datasets, the ttk.Treeview approach is recommended.

  1. Python Tkinter grid table example:

    • Description: Create a simple grid table in Tkinter using labels or entry widgets arranged in a grid.
    • Code Example:
      import tkinter as tk
      
      root = tk.Tk()
      root.title("Tkinter Grid Table Example")
      
      # Create a grid table using labels
      for i in range(5):
          for j in range(5):
              label = tk.Label(root, text=f"Row {i}, Col {j}", borderwidth=1, relief="solid")
              label.grid(row=i, column=j, padx=5, pady=5)
      
      root.mainloop()
      
  2. Tkinter treeview table example:

    • Description: Use the Tkinter ttk.Treeview widget to create a table with headers and rows.
    • Code Example:
      import tkinter as tk
      from tkinter import ttk
      
      root = tk.Tk()
      root.title("Tkinter Treeview Table Example")
      
      # Create a Treeview table
      tree = ttk.Treeview(root, columns=('Name', 'Age', 'City'))
      tree.heading('#0', text='ID')
      tree.heading('Name', text='Name')
      tree.heading('Age', text='Age')
      tree.heading('City', text='City')
      
      tree.insert('', 'end', values=('John Doe', 25, 'New York'))
      tree.insert('', 'end', values=('Jane Smith', 30, 'San Francisco'))
      
      tree.pack()
      
      root.mainloop()
      
  3. How to create a table in Tkinter:

    • Description: Create a table in Tkinter using various widgets like labels or entry widgets arranged in a grid.
    • Code Example:
      import tkinter as tk
      
      root = tk.Tk()
      root.title("Tkinter Table Example")
      
      # Create a table using labels
      for i in range(5):
          for j in range(5):
              label = tk.Label(root, text=f"Row {i}, Col {j}", borderwidth=1, relief="solid")
              label.grid(row=i, column=j, padx=5, pady=5)
      
      root.mainloop()
      
  4. Tkinter table with headers and rows:

    • Description: Create a table in Tkinter with headers and rows using labels or entry widgets arranged in a grid.
    • Code Example:
      import tkinter as tk
      
      root = tk.Tk()
      root.title("Tkinter Table with Headers and Rows")
      
      # Create headers
      headers = ['Name', 'Age', 'City']
      for j, header in enumerate(headers):
          label = tk.Label(root, text=header, borderwidth=1, relief="solid")
          label.grid(row=0, column=j, padx=5, pady=5)
      
      # Create rows
      data = [
          ['John Doe', 25, 'New York'],
          ['Jane Smith', 30, 'San Francisco']
      ]
      for i, row in enumerate(data, start=1):
          for j, value in enumerate(row):
              label = tk.Label(root, text=value, borderwidth=1, relief="solid")
              label.grid(row=i, column=j, padx=5, pady=5)
      
      root.mainloop()
      
  5. Python Tkinter table layout:

    • Description: Implement a table layout in Tkinter using labels or entry widgets arranged in a grid.
    • Code Example:
      import tkinter as tk
      
      root = tk.Tk()
      root.title("Tkinter Table Layout")
      
      # Create a table layout using labels
      for i in range(5):
          for j in range(5):
              label = tk.Label(root, text=f"Row {i}, Col {j}", borderwidth=1, relief="solid", width=15)
              label.grid(row=i, column=j, padx=5, pady=5)
      
      root.mainloop()
      
  6. Tkinter grid layout for tables:

    • Description: Utilize the grid layout in Tkinter to arrange labels or entry widgets in a table-like format.
    • Code Example:
      import tkinter as tk
      
      root = tk.Tk()
      root.title("Tkinter Grid Layout for Tables")
      
      # Create a grid layout for a table
      for i in range(5):
          for j in range(5):
              label = tk.Label(root, text=f"Row {i}, Col {j}", borderwidth=1, relief="solid", width=15)
              label.grid(row=i, column=j, padx=5, pady=5)
      
      root.mainloop()
      
  7. Creating a data table in Tkinter:

    • Description: Create a data table in Tkinter using labels or entry widgets arranged in a grid.
    • Code Example:
      import tkinter as tk
      
      root = tk.Tk()
      root.title("Tkinter Data Table Example")
      
      # Create a data table using labels
      data = [
          ['John Doe', 25, 'New York'],
          ['Jane Smith', 30, 'San Francisco']
      ]
      for i, row in enumerate(data):
          for j, value in enumerate(row):
              label = tk.Label(root, text=value, borderwidth=1, relief="solid", width=15)
              label.grid(row=i, column=j, padx=5, pady=5)
      
      root.mainloop()
      
  8. Tkinter table with scrollbars:

    • Description: Add scrollbars to a Tkinter table to handle a large number of rows or columns.
    • Code Example:
      import tkinter as tk
      from tkinter import ttk
      
      root = tk.Tk()
      root.title("Tkinter Table with Scrollbars")
      
      # Create a Treeview table with scrollbars
      tree = ttk.Treeview(root, columns=('Name', 'Age', 'City'))
      tree.heading('#0', text='ID')
      tree.heading('Name', text='Name')
      tree.heading('Age', text='Age')
      tree.heading('City', text='City')
      
      for i in range(50):
          tree.insert('', 'end', values=(f'Person {i}', i + 20, 'City {i}'))
      
      tree.pack()
      
      # Add vertical scrollbar
      v_scrollbar = ttk.Scrollbar(root, orient='vertical', command=tree.yview)
      v_scrollbar.pack(side='right', fill='y')
      tree.configure(yscrollcommand=v_scrollbar.set)
      
      root.mainloop()
      
  9. Editable table in Tkinter:

    • Description: Make a table in Tkinter editable by using entry widgets for user input.
    • Code Example:
      import tkinter as tk
      
      def on_cell_click(event, row, col):
          entry = entry_widgets[row][col]
          entry.focus_set()
      
      root = tk.Tk()
      root.title("Editable Tkinter Table")
      
      # Create an editable table using entry widgets
      entry_widgets = []
      for i in range(5):
          row_entries = []
          for j in range(5):
              entry = tk.Entry(root, borderwidth=1, relief="solid", width=15)
              entry.grid(row=i, column=j, padx=5, pady=5)
              entry.bind("<Button-1>", lambda event, row=i, col=j: on_cell_click(event, row, col))
              row_entries.append(entry)
          entry_widgets.append(row_entries)
      
      root.mainloop()