0

I am trying to bind two different functions to two different parts of the treeview. the command deleteItem should only apply to rows, whilst the columnselect function should only apply to columns. When I run this code the second bind overrides the first.

import tkinter as tk
from tkinter import messagebox
from tkinter.font import Font
from tkinter import scrolledtext
from tkinter import ttk

def deleteItem(a):
    print("In delete Item")
    curItem = AttendanceView.focus()
    inter_var = AttendanceView.item(curItem)
    ListValues = inter_var['values']
    print(ListValues)

def columnnameselect(q):
    tk.messagebox.showinfo("","You didn't press a row")
# AttendanceView Specific Table Components
Attendance=tk.Tk()
AttendanceView = ttk.Treeview(Attendance)
AttendanceView["columns"] = ("firstname", "secondname","patrol","present")
AttendanceView.grid(row=3, column=1)
AttendanceView.heading("#0", text="", anchor="w")
AttendanceView.column("#0", anchor="center", width=5, stretch=tk.NO)
AttendanceView.heading("firstname", text="First Name", anchor="w")
AttendanceView.column("firstname", anchor="center", width=80)
AttendanceView.heading("secondname", text="Second Name", anchor="w")
AttendanceView.column("secondname", anchor="center", width=90)
AttendanceView.heading("patrol", text="Patrol", anchor="w")
AttendanceView.column("patrol", anchor="center", width=80)
AttendanceView.heading("present", text="Present", anchor="w")
AttendanceView.column("present", anchor="center", width=80)
AttendanceView.grid(row=3, column=1, columnspan=5)
AttendanceView.bind('<ButtonRelease-1>', deleteItem)
AttendanceView.bind('<ButtonRelease-1>', columnnameselect)
AttendanceViewScrollbar = ttk.Scrollbar(Attendance, orient="vertical", command=AttendanceView.yview)
AttendanceView.configure(yscroll=AttendanceViewScrollbar.set)
AttendanceViewScrollbar.grid(row=3, column=6, sticky="ns")


AttendanceView.insert("", "end", text="", values=(("Ethel",("Kennedy"),("Cow"),(""))))
AttendanceView.insert("", "end", text="", values=(("Jack",("Kennedy"),("Lion"),(""))))
AttendanceView.insert("", "end", text="", values=(("Bobby",("Kennedy"),("Zebra"),(""))))

The desired outcome:

  • When a column is selected, columnselect is run.
  • When a row is selected, deleteItem is run.

Any help would be appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MainStreet
  • 129
  • 1
  • 1
  • 6
  • Not sure what you want to do exactly , but check out https://docs.python.org/3/library/tkinter.ttk.html#tkinter.ttk.Treeview.identify (and related functions like identify_column...). See also https://stackoverflow.com/questions/31584415/how-to-bind-an-action-to-the-heading-of-a-tkinter-treeview-in-python – Mekanik Dec 03 '18 at 20:45
  • @Mekanik Its hard to put this in words. Im looking for a solution such as `AttendaceView['headers'].bind('', columnselect)` – MainStreet Dec 03 '18 at 21:09

0 Answers0