I have several treeview tables.
After some moment I need to apply some property to all tables. All tables are combined in dictionary, for example:
all_trees = {1: some_tree, 2: one_more_tree, 3: another_tree}
When the moment appears I need to make this type of operation to change one property in all tables:
for key in all_trees.keys():
tree = all_trees[key]
tree.bind("<Button-3>", lambda event: new_property(event, tree))
New property makes appearing text 'hello' after right click mouse to the row in table. After executing this operation only the last table has the new property.
What's the problem? Why this property is not applied to all tables? P.S: i need to make my project using python2.7, so all my code is written using old version of python
Below is a full code:
import Tkinter as tk
from ttk import Treeview, Frame
from Tkinter import Menu, LEFT, NO, YES
data_for_tables = {1: 'one', 2: 'two', 3: 'three'}
list_of_tables = [1, 2, 3]
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
def make_tree(frame):
"""creating Treeview table"""
tree = Treeview(frame)
tree["columns"] = ("one", "two", "three")
tree.heading("#0", text="")
tree.column("#0", minwidth=0, width=5, stretch=NO)
tree.heading("one", text="Port")
tree.column("one", minwidth=0, width=30, stretch=NO)
tree.column("two", minwidth=0, width=60, stretch=NO)
tree.heading("three", text="State")
tree.column("three", minwidth=0, width=60, stretch=YES)
tree['height'] = 3
tree.pack(side=LEFT)
return tree
def insert_value(info, tree):
"""Inserting values to Treeview table"""
for key in info.keys():
tree.insert("", "end", values=(str(key), info[key], "<><><>"))
def new_property(event, tree):
"""after right ckick mouse appears text 'hello' in console"""
row_id = tree.identify_row(event.y)
if row_id:
print('hello')
global all_trees
for num in list_of_tables:
all_trees = {num: make_tree(frame)}
for key in all_trees.keys():
tree = all_trees[key]
insert_value(data_for_tables, tree)
for key in all_trees.keys():
tree = all_trees[key]
tree.bind("<Button-3>", lambda event: new_property(event, tree))
root.mainloop()
tree = all_trees.keys(), have you examinedtreeto see if it's what you think it is? I'm guessing it's not what you think it is.