tk_canvas.py 1.78 KB
Newer Older
1 2 3 4
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
5 6 7 8
    Provides structured graphics interface from Tkinter package.
    
    Name: tk_canvas.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
"""

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk
    
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import pylab as plt


class Image(object):
    
    
    def __init__(self, parent, image, onclick = None):
        self.parent = parent
        
        self.parent.image = image
            
31
        fig = plt.figure(facecolor='white', edgecolor='black', linewidth=1)
32
        self._im = plt.imshow(self.parent.image) # later use a.set_data(new_data)
33 34 35 36 37
        #ax = plt.gca()
        #ax.set_xticklabels([]) 
        #ax.set_yticklabels([])

        # a tk.DrawingArea
38 39 40
        self._canvas = FigureCanvasTkAgg(fig, master=self.parent)
        self._canvas.show()
        self._canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
41 42 43 44 45
        
        if onclick is not None:
            fig.canvas.mpl_connect('button_press_event', func = onclick)
        
    def refresh(self, image):
46
        if self._canvas is not None:
47
            self.parent.image = image
48 49
            self._im.set_data(self.parent.image)
            self._canvas.draw()
50 51 52


    def close(self):
53
        if self._canvas is not None:
54
            self.parent.image = None
55 56 57 58
            #self._im.set_data(np.zeros((0,0,3), float))
            #self._canvas.draw()
            self._canvas.get_tk_widget().pack_forget();
            self._canvas.get_tk_widget().destroy();
59
            
60 61
            self._im = None
            self._canvas = None
62 63 64
            
            return True
        return False