forked from dmnfarrell/tkintertable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_table.py
More file actions
112 lines (93 loc) · 2.64 KB
/
example_table.py
File metadata and controls
112 lines (93 loc) · 2.64 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Test script for the Tables class
"""
from __future__ import absolute_import, division, print_function
try:
from tkinter import *
from tkinter.ttk import *
except:
from Tkinter import *
from ttk import *
import random, string
from tkintertable import TableCanvas
from tkintertable import TableModel
from tkintertable import SortingPanel
"""Testing general functionality of tables"""
class App:
def __init__(self, master):
self.main = Frame(master)
self.main.pack(fill=BOTH,expand=1)
master.geometry('600x400+200+100')
def sampledata():
"""Return a sample dictionary for creating a table"""
data={}
cols = ['a','b','c','d','e']
for i in range(10):
data[i] = {i:round(random.random(),2) for i in cols}
return data
def createRandomStrings(l,n):
"""create list of l random strings, each of length n"""
names = []
for i in range(l):
val = ''.join(random.choice(string.ascii_lowercase) for x in range(n))
names.append(val)
return names
def createData(rows=20, cols=5):
"""Creare random dict for test data"""
data = {}
names = createRandomStrings(rows,16)
colnames = createRandomStrings(cols,5)
for n in names:
data[n]={}
data[n]['label'] = n
for c in range(0,cols):
colname=colnames[c]
vals = [round(random.normalvariate(100,50),2) for i in range(0,len(names))]
vals = sorted(vals)
i=0
for n in names:
data[n][colname] = vals[i]
i+=1
return data
def createTable(model, **kwargs):
t=Toplevel()
app = App(t)
master = app.main
table = TableCanvas(master, model,rowheaderwidth=50, **kwargs)
table.createTableFrame()
return table
def test1(root):
"""Setup a table and populate it with data"""
app = App(root)
master = app.main
model = TableModel()
data = createData(40)
model.importDict(data)
oframe = Frame(master)
table = TableCanvas(oframe, model,
cellwidth=60, cellbackgr='#e3f698',
thefont=('Arial',12),rowheight=18, rowheaderwidth=30,
rowselectedcolor='yellow', editable=True)
table.createTableFrame()
model.deleteColumns([0])
model.deleteRows(range(0,2))
table.addRow(1,label='aaazzz')
table.addRow(label='bbb')
table.addRow(**{'label':'www'})
table.addColumn('col6')
oframe.pack(side="top", fill="both", expand=True)
columns = list(table.model.getColumnDict().values())
sorting = SortingPanel(master, fields=columns, callback=table.triggerSorting)
sorting.pack(side="top", fill="x")
return
def GUITests():
"""Run standard tests"""
root = Tk()
test1(root)
return root
def main():
root = GUITests()
root.mainloop()
#loadSaveTest()
if __name__ == '__main__':
main()