ALEXANDAR_GEORGIEV

treeview_codemy

Oct 31st, 2022 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | Source Code | 0 0
  1. from tkinter import *
  2. from tkinter.ttk import Treeview
  3.  
  4. root = Tk()
  5. root.title('Treeview')
  6. root.geometry('800x500')
  7.  
  8. # TODO Treeview
  9. my_tree = Treeview(root)
  10.  
  11. # define Columns
  12. my_tree['columns'] = ('Name', "ID", "Favorite Pizza")
  13.  
  14. # Formate Columns
  15. my_tree.column('#0', width=0, stretch=NO)   # 0-вата колона е основния родител, Stretch=NO - скрива 0-вата колона
  16. my_tree.column('Name', anchor=W, width=120, minwidth=50)   # 1-ра колона Name
  17. my_tree.column('ID', anchor=CENTER, width=80)   # 2-ра колона ID
  18. my_tree.column('Favorite Pizza', anchor=W, width=120)   # 3-та колона Favorite Pizza
  19.  
  20. # Create Headings
  21. my_tree.heading('#0 ', text='', anchor=W)
  22. my_tree.heading('Name', text='Customer Name', anchor=W)
  23. my_tree.heading('ID', text='ID', anchor=CENTER)
  24. my_tree.heading('Favorite Pizza', text='Favorite Pizza', anchor=W)
  25.  
  26. # Add Data
  27. data = [['John', 1, 'Piperroni'],
  28.         ['Mary', 2, 'Cheese'],
  29.         ['Tim', 3, 'Mushroom'],
  30.         ['Erin', 4, 'Ham'],
  31.         ['Bob', 5, 'Onion']]
  32. count = 0
  33. for record in data:
  34.     my_tree.insert(parent='', index='end', iid=str(count), text='', values=(record[0], record[1], record[2]))
  35.     count += 1
  36.  
  37. flag1 = True
  38. pos = None
  39. pos2 = None
  40.  
  41. def show_hide_row():
  42.     global flag1, pos, pos2
  43.     children = my_tree.get_children()
  44.     iid_to_hide = "3"
  45.     iid_to_hide2 = "1"
  46.     if iid_to_hide in children:
  47.         pos = children.index(iid_to_hide)
  48.         pos2 = children.index(iid_to_hide2)
  49.     if flag1:
  50.         my_tree.detach(iid_to_hide)
  51.         my_tree.detach(iid_to_hide2)
  52.     else:
  53.         my_tree.reattach(iid_to_hide, "", pos)
  54.         my_tree.reattach(iid_to_hide2, "", pos2)
  55.     flag1 = not flag1
  56.  
  57.  
  58.  
  59. btn1 = Button(root, width=10, text='text', command=show_hide_row)
  60.  
  61. btn1.place(x=20, y=400)
  62. '''
  63. my_tree.insert(parent='', index='end', iid='0', text='Parent', values=('John', 1, 'Piperroni'))
  64. my_tree.insert(parent='', index='end', iid='1', text='', values=('Mary', '2', 'Cheese'))
  65. my_tree.insert(parent='', index='end', iid='2', text='', values=('Tina', '3', 'Ham'))
  66. my_tree.insert(parent='', index='end', iid='3', text='', values=('Bob', '4', 'Supreme'))
  67. my_tree.insert(parent='', index='end', iid='4', text='', values=('Erin', '5', 'Cheese'))
  68. my_tree.insert(parent='', index='end', iid='5', text='', values=('Wes', '6', 'Onion'))
  69. '''
  70. # Add Child
  71. my_tree.insert(parent='', index='end', iid='6', text='Child', values=('Steve', '1.2', 'Peppers'))
  72. my_tree.move('6', '0', 0)
  73.  
  74. my_tree.pack(pady=20)
  75.  
  76.  
  77.  
  78.  
  79. root.mainloop()
Add Comment
Please, Sign In to add comment