Treeview Data from Google Sheets

Tkinter Treeview with data from google sheet

Displaying Google sheet data in Tkinter Treeview using Google API using pygsheet


We are collecting data from google sheets and using as rows of a Treeview

Authorization and connection using Google API
import pygsheets
path='G:\\My drive\\testing\\google-sheet\\creds1.json'
gc=pygsheets.authorize(service_account_file=path)
sh=gc.open('my_gsheets1')
wk1=sh[0]
l1=wk1.get_all_values(include_tailing_empty=False) # list with data
Using the above list ( l1 ) we will populate the Treeview. Here is our Treeview. We created with columns and headers matching to our google sheets data.
Tkinter Treeview Display Data in Treeview
import pygsheets
path='G:\\My drive\\testing\\google-sheet\\creds1.json'
gc=pygsheets.authorize(service_account_file=path)
sh=gc.open('my_gsheets1')
wk1=sh[0]
l1=wk1.get_all_values(include_tailing_empty=False) # list with data
#print(l1)
# Starting of Tkinter window code. 
from tkinter import ttk
import tkinter as tk
# Creating tkinter my_w
my_w = tk.Tk()
my_w.geometry("400x400") 
my_w.title("www.plus2net.com")  
# Using treeview widget
trv = ttk.Treeview(my_w, selectmode ='browse')
trv.grid(row=1,column=1,padx=20,pady=20)
# number of columns
trv["columns"] = ("1", "2", "3","4","5")
  
# Defining heading
trv['show'] = 'headings'
trv['height']=15 # Number of rows to display by default. 
# width of columns and alignment 
trv.column("1", width = 30, anchor ='c')
trv.column("2", width = 80, anchor ='c')
trv.column("3", width = 80, anchor ='c')
trv.column("4", width = 80, anchor ='c')
trv.column("5", width = 80, anchor ='c')
  
# Headings  
# respective column heading are taken from google sheets
# if only data is taken from google sheet then below lines can be added
#trv.heading("1", text ="id")
#trv.heading("2", text ="Name")
#trv.heading("3", text ="Class")
#trv.heading("4", text ="Mark")  
#trv.heading("5", text ="Gender")
#trv['displaycolumns']=["1","2","3"]

for dt in l1:  # l1 is the list having google sheets data
    trv.insert("", 'end',iid=dt[0], text=dt[0],
               values =(dt[0],dt[1],dt[2],dt[3],dt[4]))

my_w.mainloop()
Pygsheets and google API authorization Python Tkinter Combobox
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer