-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy patheditgrid.py
More file actions
101 lines (66 loc) · 2.48 KB
/
Copy patheditgrid.py
File metadata and controls
101 lines (66 loc) · 2.48 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
# -*- coding: utf-8 -*-
import wx
import wx.grid
import numpy as np
class EditGrid(wx.grid.Grid):
def __init__(self, *args, **kwargs):
wx.grid.Grid.__init__(self, *args, **kwargs)
wx.EVT_KEY_DOWN(self, self.OnKeyDown)
def OnKeyDown(self, event):
key = event.GetKeyCode()
if event.ControlDown and key == ord('V'):
self.OnPaste(event)
else:
event.Skip()
def toarray(self, selection=None):
if selection:
x0, y0, x1, y1 = selection
else:
x0, y0, x1, y1 = self._getvalidbounds()
out = np.zeros([x1-x0, y1-y0], 'd')
for i in range(x0, x1):
for j in range(y0, y1):
out[i,j]= float(self.GetCellValue(i,j))
return out
def _getvalidbounds(self):
x0 = 0
y0 = 0
x1 = 0
y1 = 0
while y1 <= self.GetNumberCols() and not self.GetCellValue(0, y1) == '':
y1 += 1
while x1 <= self.GetNumberRows() and not self.GetCellValue(x1, 0) =='':
x1 += 1
return x0, y0, x1, y1
def setarray(self, data,x0=0, y0=0):
for i in range(data.shape[0]):
for j in range(data.shape[1]):
self.SetCellValue(i+x0, j+y0, '%s' % data[i, j])
def tostring(self, selection=None):
from io import BytesIO
sb = BytesIO()
np.savetxt(sb, self.toarray(selection), delimiter='\t')
return sb.getvalue()
def setfromstring(self, data, x0=0, y0=0):
from io import BytesIO
#print repr(data)
sb = BytesIO(data.encode())
self.setarray(np.loadtxt(sb, delimiter = '\t'), x0, y0)
def OnPaste(self, event):
cb = wx.TextDataObject()
wx.TheClipboard.Open()
wx.TheClipboard.GetData(cb)
wx.TheClipboard.Close()
self.setfromstring(cb.GetText())
class EntryGrid(wx.Frame):
def __init__(self, parent=None):
wx.Frame.__init__(self, parent, size=(500, 500))
self.grid = EditGrid(self)
self.grid.CreateGrid(100, 5)
@property
def data(self):
return self.grid.toarray()
def ShowDataGrid():
f = EntryGrid()
f.Show()
return f