0

I am trying to improve the functionality of my GUI by adding buttons to the headings but can't find any examples or information on this one. Is there a way to make the headings into a clickable button in this example? The Treeview is going to be populated from a DB so, if I can get the buttons to work, it means I can order the display in a nicer way than having more buttons outside the "box".

import tkinter as tk
from tkinter import ttk

screen = tk.Tk() 
screen.title('This One')
screen.geometry('890x400')
style = ttk.Style()
style.theme_use("clam")
screen.grid_rowconfigure(1, weight=1)
screen.grid_columnconfigure(0, weight=1)

cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
    box.heading(col, text=col)
    box.grid(row=1, column=0, columnspan=2,sticky='nsew')


box.column("TOKEN", width=95)
box.column("F-500", width=85, anchor='e')
box.column("F-250", width=85, anchor='e')
box.column("F-100", width=85, anchor='e')
box.column("F-24", width=85, anchor='e')
box.column("POS", width=75, anchor='center')
box.column("NEG", width=75, anchor='center')



closeButton = tk.Button(screen, text="Close", width=15, command=exit).grid(row=10, column=0)

screen.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Morty2
  • 7
  • 3
  • is `box.heading(col, text=col, command= lambda:print('hi'))` what you are asking for? – Thingamabobs Sep 26 '20 at 10:41
  • For how lambda works follow this: https://stackoverflow.com/questions/16501/what-is-a-lambda-function/62742314#62742314 – Thingamabobs Sep 26 '20 at 10:42
  • That is a start! that gives all the heading "buttons" a function but how would I get each one to have a seperate function? for example print("hi") when "token is clicked but print("bye") when f-500 is clicked... – Morty2 Sep 26 '20 at 10:52
  • @Monty2 see my answer below – Thingamabobs Sep 26 '20 at 10:54

1 Answers1

0

The heading od the Treeview already are buttons with the optional keyword command. To use this in a forloop you can use lambda an annoynmous function.

import tkinter as tk
from tkinter import ttk

screen = tk.Tk() 
screen.title('This One')
screen.geometry('890x400')
style = ttk.Style()
style.theme_use("clam")
screen.grid_rowconfigure(1, weight=1)
screen.grid_columnconfigure(0, weight=1)

cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
    if col == 'TOKEN':
        box.heading(col, text=col, command =lambda: print('token'))
    box.grid(row=1, column=0, columnspan=2,sticky='nsew')


box.column("TOKEN", width=95)
box.column("F-500", width=85, anchor='e')
box.column("F-250", width=85, anchor='e')
box.column("F-100", width=85, anchor='e')
box.column("F-24", width=85, anchor='e')
box.column("POS", width=75, anchor='center')
box.column("NEG", width=75, anchor='center')



closeButton = tk.Button(screen, text="Close", width=15, command=exit).grid(row=10, column=0)

screen.mainloop()

without using lambda:

def token():
    print('token')

cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
    if col == 'TOKEN':
        box.heading(col, text=col, command =token)
    box.grid(row=1, column=0, columnspan=2,sticky='nsew')
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • 1
    I see!! Thanks for that, i just need to expand the if statement to include all the cols and then attach the functions! Thank you so much for your help! – Morty2 Sep 26 '20 at 10:57