Can we add checkbox and custom right click commands to Treeview? #255
-
Hi, First of all, Thanks for the great library. I design UI using ttkbootstrap Is it possible to add checkbox to the treeview and custom right click commands, if yes, then please let me know how to. Thanks Regards, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can use a hack to add a Radiobutton, but it is just a hack. Then, to add a right-click event, you can use the example below as well. You'll need to create a callback and then bind it to the widget. import ttkbootstrap as ttk
from ttkbootstrap.constants import HEADINGS, END
CHECKBOX_COLUMN = 2
window = ttk.Window()
tv = ttk.Treeview(columns=[0, 1, 2], show=HEADINGS)
tv.pack(padx=10, pady=10)
tv.heading(0, text="Description")
tv.heading(1, text="Item Number")
tv.heading(CHECKBOX_COLUMN, text="Checkboxes")
# add several items to the treeview
records = []
for x in range(10):
item = tv.insert('', END, values=('Some random item', x))
records.append(item)
def initialize_checkbuttons(records):
"""Add checkbuttons to the table. The position of the treeview item can only
be found if the table is visible, so this method will be called after the window
is visible."""
for record in records:
checkbox = ttk.Checkbutton(tv, text=f"checkbox for {record}")
x, y, width, height = tv.bbox(record, CHECKBOX_COLUMN)
checkbox.place(x=x, y=y, width=width, height=height)
# create right-click event
def on_right_click(event):
print(event)
print('do something here')
# add right-click event to treeview
tv.bind('<Button-3>', on_right_click)
# Add the checkbuttons AFTER the window is made visible
window.after(500, initialize_checkbuttons, records)
window.mainloop() |
Beta Was this translation helpful? Give feedback.
-
Thanks alot israel-dryer |
Beta Was this translation helpful? Give feedback.
You can use a hack to add a Radiobutton, but it is just a hack.
Then, to add a right-click event, you can use the example below as well. You'll need to create a callback and then bind it to the widget.