buffer.py 6.62 KB
Newer Older
Julio Tambara's avatar
Julio Tambara committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')
import os
import sys
import pexpect
import numpy as np
import Tkinter as tk
import tkMessageBox
import tkFileDialog
import matplotlib.pyplot as plt
import time
from pylab import *
from os.path import basename
#from clsbeep import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.figure import Figure

25

Julio Tambara's avatar
Julio Tambara committed
26
27
28
29
30
31
32
import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk


33

Julio Tambara's avatar
Julio Tambara committed
34
35
36
37
38
39
40
41
42
Plot=dict( Data=dict(), Plotbuttons=list(), Pcontrol=list(), Name=list() )
Interface = dict(frames=list(), buttons=list(), labels=list(), spinboxes=list(), entries=list(), labelframes=list())

event=dict(last_x=int ,last_y=int)
j=0 #numero de botoes (facilita)
n=0;#numero de botoes removidos (facilita)



43

Julio Tambara's avatar
Julio Tambara committed
44
45
46
47
48
49
Axis=dict( x_max=int , x_min=int, y_max=int, y_min=int, x_click=float, y_click=float)

root = Tk.Tk()
root.minsize(825,640)
root.wm_title("Buffer")

50
51
52
53
54
55
56
57
58
59
def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
                    # Fatal Python Error: PyEval_RestoreThread: NULL tstate






Julio Tambara's avatar
Julio Tambara committed
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

f = plt.Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
a.set_xlabel('Channels')
a.set_ylabel('Counts')
f.suptitle('Buffer')


########################################################################
########################################################################
def refresh():
    global  Plot
    a.cla()
    for i in range(len(Plot['Plotbuttons'])):
        if Plot['Pcontrol'][i]:            
            a.plot(Plot['Data'][i][:,0],Plot['Data'][i][:,1], label=Plot['Name'][i])   #Plot['Data'][i][0],Plot['Data'][i][1])        
    a.autoscale_view()
    a.legend()  
    f.canvas.draw()       

def plot_select(name_plot, button):
    global Plot
    j=Plot['Name'].index(name_plot);
    if Plot['Pcontrol'][j]:
        button[j]['relief']='raised'
        Plot['Pcontrol'][j] = 0
    else:
        button[j]['relief']='sunken'
        Plot['Pcontrol'][j] = 1
    refresh()

91
def add_plot(Name):
Julio Tambara's avatar
Julio Tambara committed
92
    global j, Plot
93
    Plot['Data'][j] = np.loadtxt(Name)    
Julio Tambara's avatar
Julio Tambara committed
94
    Plot['Pcontrol'].insert(j,1)
95
96
    Plot['Name'].insert(j,basename(Name)) 
    Plot['Plotbuttons'].insert(j, tk.Button(pltbutfrm, text=basename(Name), height=1,relief='sunken', command = lambda j=j : plot_select(basename(Name),Plot['Plotbuttons'])))
Julio Tambara's avatar
Julio Tambara committed
97
98
    #Plot['Plotbuttons'][j].grid( row = len(Plot['Plotbuttons']))
    Plot['Plotbuttons'][j].pack(fill='x')
99
    remove_menu.add_command(label=basename(Name),  command=lambda:remove_plot(basename(Name)))
Julio Tambara's avatar
Julio Tambara committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    j = j + 1
    
    refresh()

def remove_plot(name_plot):
    global j, Plot, n

    i=Plot['Name'].index(name_plot);
    del  Plot['Data'][i] 
    Plot['Pcontrol'].pop(i)
    Plot['Name'].pop(i)
    Plot['Plotbuttons'][i].pack_forget() 
    Plot['Plotbuttons'].pop(i)
    remove_menu.delete(i)
    
       
    for x in range(j-i-1):
        Plot['Data'][x+i]= Plot['Data'][x+i+1]

    j=j-1
    n=n+1
    refresh()


124

Julio Tambara's avatar
Julio Tambara committed
125
126
127
128
129
130
########################################################################
########################################################################
# Menu

menubar = Tk.Menu(root, relief='raised', bd=2)
filemenu = Tk.Menu(menubar, tearoff=0)
131
filemenu.add_command(label="Open",command = lambda:add_plot(tkFileDialog.askopenfilename()))
Julio Tambara's avatar
Julio Tambara committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
filemenu.add_command(label ="Save")       #, command = lambda i=i: save('g12'))
filemenu.add_command(label ="Save As")    #, command = lambda i=i: saveas('g12'))
filemenu.add_command(label="Exit",command = lambda:_quit())        #, command = _quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)

Interface['frames'].insert(0,tk.Frame(root,takefocus=0)) #Major frame
Interface['frames'][0].place(anchor='nw',relheight=1, relwidth=1)
Interface['frames'].insert(1,tk.Frame(Interface['frames'][0],takefocus=0)) #north west
Interface['frames'][1].place(anchor='nw',relheight=0.80, relwidth=0.80)
Interface['frames'].insert(2,tk.Frame(Interface['frames'][0],takefocus=0,relief='ridg',borderwidth=2)) #Bot  and left 
Interface['frames'][2].place(anchor='nw',relheight=0.20, relwidth=0.20,relx = 0.8,rely=0.8)
Interface['frames'].insert(3,tk.Frame(Interface['frames'][0],takefocus=0)) #top and right
Interface['frames'][3].place(anchor='nw',relheight=0.80, relwidth=0.20,relx = 0.8)
Interface['frames'].insert(4,tk.Frame(Interface['frames'][0],takefocus=0)) #bot and right
Interface['frames'][4].place(anchor='nw',relheight=0.20, relwidth=0.80,rely=0.8)

pltbutfrm =tk.Frame(Interface['frames'][3],takefocus=0)
Interface['frames'].insert(5,pltbutfrm) #frame que contem os botoes
Interface['frames'][5].pack(fill='x')



Interface['labels'].insert (0,tk.Label(Interface['frames'][2], text='Buffer'))
Interface['labels'][0].pack(fill='x')
Interface['labels'].insert (1,tk.Label(Interface['frames'][3], text='ROI'))
Interface['labels'][1].pack(fill='x')
Interface['labels'].insert (2,tk.Label(Interface['frames'][4], text='Test'))
Interface['labels'][2].pack(fill='x')


##############
#rig_but_menu#
##############
rig_but_menu= tk.Menu(root, tearoff=0)
rig_but_menu.add_command(label="Load")
rig_but_menu.add_command(label="Click/Unclick")


remove_menu = Tk.Menu(rig_but_menu, tearoff=0)

rig_but_menu.add_cascade(label="Remove plot", menu=remove_menu)


def mouse_TR(event):
    rig_but_menu.tk_popup(event.x_root, event.y_root)




# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=Interface['frames'][1])
canvas.get_tk_widget().place(anchor='nw',relheight=1, relwidth=1)
#canvas.show()
#canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

#toolbar = NavigationToolbar2TkAgg( canvas, root )
#toolbar.update()
#canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

Interface['frames'][3].bind("<ButtonRelease-3>", mouse_TR )#top_right



########################################################################
########################################################################
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
try:
    add_plot('Buffer.MCA')

except IOError:
   print tkMessageBox.askretrycancel("ERRO", "ERRO_10 ")
   _quit()
try:
    add_plot('Buffer.MCB')
except IOError:
   None
try:
    add_plot('Buffer.MCC')
except IOError:
   None


Julio Tambara's avatar
Julio Tambara committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232




    #newfile = tkFileDialog.askopenfilename()
    #filefile = np.loadtxt(newfile)
    #tempfile = open('save.dat','w+')
    #set_fator(len(filefile))
    #for h in filefile[:,1]:
    #    tempfile.write(str(int(h))+'\n')
    #tempfile.close()
    



Tk.mainloop()
# If you put root.destroy() here, it will cause an error if
# the window is closed with the window manager.