Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter.ttk import Treeview
- root = Tk()
- root.title('Treeview')
- root.geometry('800x500')
- # TODO Treeview
- my_tree = Treeview(root)
- # define Columns
- my_tree['columns'] = ('Name', "ID", "Favorite Pizza")
- # Formate Columns
- my_tree.column('#0', width=0, stretch=NO) # 0-вата колона е основния родител, Stretch=NO - скрива 0-вата колона
- my_tree.column('Name', anchor=W, width=120, minwidth=50) # 1-ра колона Name
- my_tree.column('ID', anchor=CENTER, width=80) # 2-ра колона ID
- my_tree.column('Favorite Pizza', anchor=W, width=120) # 3-та колона Favorite Pizza
- # Create Headings
- my_tree.heading('#0 ', text='', anchor=W)
- my_tree.heading('Name', text='Customer Name', anchor=W)
- my_tree.heading('ID', text='ID', anchor=CENTER)
- my_tree.heading('Favorite Pizza', text='Favorite Pizza', anchor=W)
- # Add Data
- data = [['John', 1, 'Piperroni'],
- ['Mary', 2, 'Cheese'],
- ['Tim', 3, 'Mushroom'],
- ['Erin', 4, 'Ham'],
- ['Bob', 5, 'Onion']]
- count = 0
- for record in data:
- my_tree.insert(parent='', index='end', iid=str(count), text='', values=(record[0], record[1], record[2]))
- count += 1
- flag1 = True
- pos = None
- pos2 = None
- def show_hide_row():
- global flag1, pos, pos2
- children = my_tree.get_children()
- iid_to_hide = "3"
- iid_to_hide2 = "1"
- if iid_to_hide in children:
- pos = children.index(iid_to_hide)
- pos2 = children.index(iid_to_hide2)
- if flag1:
- my_tree.detach(iid_to_hide)
- my_tree.detach(iid_to_hide2)
- else:
- my_tree.reattach(iid_to_hide, "", pos)
- my_tree.reattach(iid_to_hide2, "", pos2)
- flag1 = not flag1
- btn1 = Button(root, width=10, text='text', command=show_hide_row)
- btn1.place(x=20, y=400)
- '''
- my_tree.insert(parent='', index='end', iid='0', text='Parent', values=('John', 1, 'Piperroni'))
- my_tree.insert(parent='', index='end', iid='1', text='', values=('Mary', '2', 'Cheese'))
- my_tree.insert(parent='', index='end', iid='2', text='', values=('Tina', '3', 'Ham'))
- my_tree.insert(parent='', index='end', iid='3', text='', values=('Bob', '4', 'Supreme'))
- my_tree.insert(parent='', index='end', iid='4', text='', values=('Erin', '5', 'Cheese'))
- my_tree.insert(parent='', index='end', iid='5', text='', values=('Wes', '6', 'Onion'))
- '''
- # Add Child
- my_tree.insert(parent='', index='end', iid='6', text='Child', values=('Steve', '1.2', 'Peppers'))
- my_tree.move('6', '0', 0)
- my_tree.pack(pady=20)
- root.mainloop()
Add Comment
Please, Sign In to add comment