-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeview_functions.py
86 lines (56 loc) · 1.92 KB
/
treeview_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pandas as pd
import tkinter as tk
def get_columns_values(treeview, column):
values = []
children = treeview.get_children()
for child in children:
if column == 0:
values.append(treeview.item(child, 'text'))
else:
values.append((treeview.item(child, 'values'))[column-1])
return values
def get_all_treeview_items(treeview):
treeview_data = []
children = treeview.get_children()
for child in children:
treeview_data.append([treeview.item(child, 'text')])
values = treeview.item(child, 'values')
for x in values:
treeview_data[-1].append(x)
return treeview_data
def write_data_to_treeview(treeview, mode, data):
if mode == 'replace':
treeview.delete(*treeview.get_children())
for d in data:
if len(d) > 1:
treeview.insert('', 'end', text=d[0], values=tuple(d[1:]))
else:
treeview.insert('', 'end', text=d[0])
def get_treeview_headers(treeview):
columns = list(treeview['columns'])
columns.insert(0, '#0')
return [treeview.heading(x)['text'] for x in columns]
def treeview_to_df(treeview):
treeview_data = {}
#Columns
headers = get_treeview_headers(treeview)
for column, h in enumerate(headers):
treeview_data[h] = get_columns_values(treeview, column)
df = pd.DataFrame.from_dict(treeview_data)
return df
def del_selected_items(treeview, msg = False):
selected_item = treeview.selection()
if selected_item:
if msg: #if we require a messagebox
MsgBox = tk.messagebox.askquestion ('Delete Selected Item??','This Cannont Be Undone!',icon = 'warning')
if MsgBox == 'yes':
treeview.delete(selected_item)
else:
treeview.delete(selected_item)
return MsgBox
def get_current_selection(treeview):
item = treeview.selection()[0]
index = treeview.index(item)
data = list(treeview.item(item,"values"))
data.insert(0, treeview.item(item,"text"))
return index, data