Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Tutorials listed in this section have not been reviewed nor are affliated by Code Forum. It is the sole responsbility of the authour to properly maintain and ensure information is accurate.

Python tkinter ttk treeview example

Basic example of tkinter ttk treeview with alternating row colors and styles text

Python:
# Do the imports
import tkinter as tk
from tkinter import ttk, font

# Initiate the root window
root = tk.Tk()

# Setting min size
root.minsize(400,300)

# Get some data for inserting
fonts = list(font.families())
fonts.sort()

# Name the columns
columns = ('fonts')

# Configuring for fonts
s = ttk.Style()
s.configure('AStyle.Treeview', rowheight=35)

# Create the treeview
tree = ttk.Treeview(root, columns=columns, show='headings', style='AStyle.Treeview')

# The tree headers
tree.heading('fonts', text='Fonts')
tree.pack(side='left', fill='both', expand='yes')

# Configure tags for alternating colors
tree.tag_configure('odd', background='lightblue')
tree.tag_configure('even', background='skyblue')

# Create the scrollbar
scrollbar = ttk.Scrollbar(root, orient='vertical', command=tree.yview)
tree.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side='right', fill='y')

# Loop through data for inserting
for index, font in enumerate(fonts):
    # Configure fonts for display and insert data
    tree.tag_configure(str(font), font=(str(font), 14, 'normal'))
    if index % 2 == 0:
        tree.insert('', 'end', values=font, tags=('even', font,))
    else:
        tree.insert('', 'end', values=font, tags=('odd', font, ))


root.mainloop()
 
Back
Top Bottom