1+ # Python Tkinter Treeview III Color and Style
2+ # Vista de árbol de Python Tkinter
3+
4+ from tkinter import *
5+ from tkinter import ttk
6+
7+ root = Tk ()
8+ root .title ('Python Tkinter Treeview III Color and Style' )
9+ root .iconbitmap ('Python Tkinter Treeview III Color and Style/Icons/cocoa.ico' )
10+ root .geometry ("500x600" )
11+
12+ # Add some style
13+ style = ttk .Style ()
14+
15+ # Pick a theme
16+ style .theme_use ("default" )
17+
18+ # Configure our treeview colors
19+ style .configure ("Treeview" ,
20+ background = "silver" ,
21+ foreground = "black" ,
22+ rowheight = 25 ,
23+ fieldbackground = "silver"
24+ )
25+
26+ # CHange selected color
27+ style .map ('Treeview' ,
28+ background = [('selected' , 'blue' )])
29+
30+ my_tree = ttk .Treeview (root )
31+
32+ # Define Our Columns
33+ my_tree ["columns" ] = ("Name" , "ID" , "Favorite Pizza" )
34+
35+ # Formate Our Columns
36+ my_tree .column ("#0" , width = 0 , stretch = NO )
37+ my_tree .column ("Name" , anchor = W , width = 140 )
38+ my_tree .column ("ID" , anchor = CENTER , width = 100 )
39+ my_tree .column ("Favorite Pizza" , anchor = CENTER , width = 140 )
40+
41+ # Create Headings
42+ my_tree .heading ("#0" , text = "" , anchor = W )
43+ my_tree .heading ("Name" , text = "Name" , anchor = W )
44+ my_tree .heading ("ID" , text = "ID" , anchor = CENTER )
45+ my_tree .heading ("Favorite Pizza" , text = "Favorite Pizza" , anchor = W )
46+
47+
48+ # Add Data
49+ data = [
50+
51+ ["John" , 1 , "Pepperoni" ],
52+ ["Mary" , 2 , "Cheese" ],
53+ ["Tim" , 3 , "Mushroom" ],
54+ ["Erin" , 4 , "Ham" ],
55+ ["Bob" , 5 , "Orion" ]
56+ ]
57+
58+ count = 0
59+ for record in data :
60+ my_tree .insert (parent = '' , index = 'end' , iid = count , text = "" , values = (record [0 ], record [1 ], record [2 ] ))
61+ count += 1
62+
63+ '''
64+ my_tree.insert(parent='', index='end', iid=0, text="", values=("Jhon", 1, "Peperroni"))
65+ my_tree.insert(parent='', index='end', iid=1, text="", values=("Mary", "2", "Cheese"))
66+ my_tree.insert(parent='', index='end', iid=2, text="", values=("Tina", "3", "Ham"))
67+ my_tree.insert(parent='', index='end', iid=3, text="", values=("Bob", "4", "Supreme"))
68+ my_tree.insert(parent='', index='end', iid=4, text="", values=("Erin", "5", "Cheese"))
69+ my_tree.insert(parent='', index='end', iid=5, text="", values=("Wes", "6", "Onion"))
70+ '''
71+ #add child
72+ #my_tree.insert(parent='', index='end', iid=6, text="Child", values=("Steve", "1.2", "Peppers"))
73+ #my_tree.move("6", "0", "0")
74+
75+ # Pack to the screen
76+ my_tree .pack (pady = 20 )
77+
78+ add_frame = Frame (root )
79+ add_frame .pack (pady = 20 )
80+
81+ # Labels
82+ n1 = Label (add_frame , text = "Name" )
83+ n1 .grid (row = 0 , column = 0 )
84+
85+ il = Label (add_frame , text = "ID" )
86+ il .grid (row = 0 , column = 1 )
87+
88+ tl = Label (add_frame , text = "Topping" )
89+ tl .grid (row = 0 , column = 2 )
90+
91+ # Entry Boxes
92+ name_box = Entry (add_frame )
93+ name_box .grid (row = 1 , column = 0 )
94+
95+ id_box = Entry (add_frame )
96+ id_box .grid (row = 1 , column = 1 )
97+
98+ topping_box = Entry (add_frame )
99+ topping_box .grid (row = 1 , column = 2 )
100+
101+ # Add Records
102+ def add_records ():
103+ global count
104+
105+ my_tree .insert (parent = '' , index = 'end' , iid = count , text = "" , values = (name_box .get (), id_box .get (), topping_box .get ()))
106+ count += 1
107+
108+ # Clear the boxes
109+ name_box .delete (0 , END )
110+ id_box .delete (0 , END )
111+ topping_box .delete (0 , END )
112+
113+ # Remove all Recods
114+ def remove_all ():
115+
116+ for record in my_tree .get_children ():
117+ my_tree .delete (record )
118+
119+ # Remone One selected
120+ def remove_one ():
121+ x = my_tree .selection ()[0 ]
122+ my_tree .delete (x )
123+
124+ # Remove many selected
125+ def remove_many ():
126+ x = my_tree .selection ()
127+ for record in x :
128+ my_tree .delete (record )
129+
130+
131+
132+
133+ # Buttons
134+ add_rercord = Button (root , text = "Add Record" ,command = add_records )
135+ add_rercord .pack (pady = 20 )
136+
137+ # Remove All
138+ remove_All = Button (root , text = "Remove All" ,command = remove_all )
139+ remove_All .pack (pady = 10 )
140+
141+ # Remove One
142+ remove_one = Button (root , text = "Remove One Selected" , command = remove_one )
143+ remove_one .pack (pady = 10 )
144+
145+ # Remove many Selected
146+ remove_many = Button (root , text = "Remove Many Selected" , command = remove_many )
147+ remove_many .pack (pady = 10 )
148+
149+
150+ root .mainloop ()
0 commit comments