Reputation: 15098
Here i have a code and i want to have different column width to different columns,is there a way to do this? Here the width is 120 but i dont want all columns to have 120 width jus a specific column and i want others columns to have different wdith? Any suggestion? Thanks in advance :)
Code :
def all_logs():
log = Toplevel(root)
log.transient(root)
log.title('View all Visitors')
# setup treeview
columns = ('ID', 'S_ID', 'S_NAME', 'B_NAME', 'Date_Taken', 'Due_Date','Date_Returned', 'Status')
tree = ttk.Treeview(log, height=20, columns=columns, show='headings')
tree.grid(row=0, column=0, sticky='news')
# setup columns attributes
for col in columns:
tree.heading(col, text=col)
tree.column(col, width=120, anchor=tk.CENTER)
# fetch data
con = mysql.connect(host='localhost', user='root', password='*****', database='DB')
c = con.cursor()
c.execute('SELECT * FROM library')
# populate data to treeview
for rec in c:
tree.insert('', 'end', value=rec)
# scrollbar
sb = tk.Scrollbar(log, orient=tk.VERTICAL, command=tree.yview)
sb.grid(row=0, column=1, sticky='ns')
tree.config(yscrollcommand=sb.set)
a = tree.item(tree.focus())['values']
btn = tk.Button(log, text='Close', command=log.destroy, width=20, bd=2, fg='red')
btn.grid(row=1, column=0, columnspan=2)
con.close()
Upvotes: 0
Views: 1402
Reputation: 47153
You can put the required widths alongside with the column names:
columns = (
('ID', 50),
('S_ID', 50),
('S_NAME', 120),
('B_NAME', 120),
('Date_Taken', 100),
('Due_Date', 100),
('Date_Returned', 100),
('Status', 50),
)
Then you need to extract the column names when creating the treeview:
tree = ttk.Treeview(log, height=20, columns=[x[0] for x in columns], show='headings')
And configure the column headings and widths:
for col, width in columns:
tree.heading(col, text=col)
tree.column(col, width=width, anchor=tk.CENTER)
Upvotes: 1