
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.
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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.