TkInterface.py 7.84 KB
Newer Older
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
  Nome: TkInterface.py
  Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )

  Descricão: Classe que implementa os modulos de interface utilizando a biblioteca TkInter.
"""
import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk
import tkMessageBox

import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import pylab as plt

from Interface import Interface, InterfaceException as IException
from tk_interface import *

class TkInterface(Interface):
    
    utils = TkUtils.TkUtils

    title = None
    
    __root = None
    
    __menus = None
    __configs = None
    
    __im = None
    __canvas = None
    
    __logger = None
    
    __dialog = None


    def __init__(self, title):
        self.title = title
        
        self.__root = Tk.Tk()
        self.__root.wm_title(self.title)
        self.__root.geometry('%dx%d+%d+%d' % (800, 600, 0, 0))
        
        self.__menus = []
        self.__configs = []
        
    def set_subtitle(self, subtitle):
        self.__root.wm_title(self.title + ' - ' + subtitle)
    

    def add_menu(self, label):
        self.__menus.append( Menu.Menu(label) )
    
    def add_command(self, label, action, shortcut = None):
        self.__menus[-1].add_command( label = label, action = lambda *_: self.__apply( action ), shortcut = shortcut )

    def add_separator(self):
        self.__menus[-1].add_separator( )

    def render_menu(self):
        menubar = Tk.Menu(self.__root)
        
        shortcuts = []
        for menu in self.__menus:
            menubar = menu.render(menubar)
            shortcuts += menu.get_shorcuts()
          
        self.__root.config(menu=menubar)
        
        for sh in shortcuts:
            self.__root.bind("<Control-Key-" + sh["shortcut"] + ">", sh["action"])
            self.__root.bind("<Control-Key-" + sh["shortcut"].lower() + ">", sh["action"])

    
    def show(self):
        self.__root.protocol("WM_DELETE_WINDOW", self.quit)
        self.__root.mainloop()
        
    
    def add_image(self, image, title = None, onclick = None):
        if self.__canvas is None:
            self.__root.image = image
            
            fig = plt.figure(facecolor='white')
            self.__im = plt.imshow(self.__root.image) # later use a.set_data(new_data)
            #ax = plt.gca()
            #ax.set_xticklabels([]) 
            #ax.set_yticklabels([])

            # a tk.DrawingArea
            self.__canvas = FigureCanvasTkAgg(fig, master=self.__root)
            self.__canvas.show()
            self.__canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
            
            if onclick is not None:
                fig.canvas.mpl_connect('button_press_event', func = lambda event, *_: self.__apply( onclick, event ))
            
            if title is not None:
                self.set_subtitle(title)

        else:
            self.refresh_image(image, title)
        
    def refresh_image(self, image, title = 'None'):
        if self.__canvas is not None:
            self.__root.image = image
            self.__im.set_data(self.__root.image)
            self.__canvas.draw()

    def close_image(self):
        if self.__canvas is not None:
            if tkMessageBox.askokcancel("Quit", "Do you want to close the image?"):
                self.__root.image = None
                #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();
                
                self.__im = None
                self.__canvas = None
                
                return True
        return False
                
                
    def create_logger(self):
        self.__logger = Tk.Text(self.__root, width=0, height=10, bg="white", fg="black", padx=5, pady=5)
        self.__logger.insert(Tk.INSERT, "$ Pynovisao is ready!\n")
        self.__logger.config(state=Tk.DISABLED)
        self.__logger.pack(side=Tk.BOTTOM, fill=Tk.X, expand=False)
        
    def write_logger(self, fmt, *args):
        self.clean_logger()
        self.append_logger(fmt % args)
        
    def append_logger(self, fmt, *args):
        self.__logger.config(state=Tk.NORMAL)
        self.__logger.insert(Tk.END, fmt % args)
        self.__logger.insert(Tk.END, '\n')
        self.__logger.config(state=Tk.DISABLED)
        
    def clean_logger(self):
        self.__logger.config(state=Tk.NORMAL)
        self.__logger.delete('1.0', Tk.END)
        self.__logger.insert('1.0', '$ ')
        self.__logger.config(state=Tk.DISABLED)
        
    def destroy_logger(self):
        self.__logger.pack_forget();
        self.__logger.destroy();
        

    def popup(self):
        pass
        
    def log_and_popup(self, message):
        pass
        
    
    def show_error(self, fmt, *args):
        tkMessageBox.showerror("Error", fmt % args)
        
    def show_info(self, fmt, *args):
        tkMessageBox.showinfo("Info", fmt % args)
        
    def show_warning(self, fmt, *args):
        tkMessageBox.showwarning("Warning", fmt % args)


    def dialogue_config(self, title, configs, callback):
        n_configs = len(configs)
        
        self.__dialog = Tk.Toplevel(self.__root, padx=10, pady=10)
        self.__dialog.title(title)
        
        row = 0
        self.__configs = []
        for config in configs:
            Tk.Label(self.__dialog, text=config.title()).grid(row=row, padx=4, pady=4, sticky=Tk.W)
            
            entry = Tk.Entry(self.__dialog)
            entry.insert(0, str(configs[config].value))
            entry.grid(row=row, column=1, padx=4, pady=4, sticky=Tk.W)
            if row == 0:
                entry.focus_set()
            
            self.__configs.append( TkConfig.TkConfig(config, configs[config].value, c_type=configs[config].c_type, tk_entry=entry) )
            row += 1

        B1 = Tk.Button(self.__dialog, text="Ok", width=5, command = lambda *_: self.__apply( callback ))
        B1.grid(row=row, padx=6, pady=6, sticky=Tk.W)
        
        self.__dialog.bind("<Return>", lambda *_: self.__apply( callback ))
        
        B2 = Tk.Button(self.__dialog, text="Cancel", width=5, command = self.__dialog.destroy)
        B2.grid(row=row, column=1, padx=6, pady=6, sticky=Tk.W)
        
        self.__dialog.bind("<Escape>", lambda *_: self.__dialog.destroy())
        
        self.__dialog.grab_set()

        self.__dialog.protocol("WM_DELETE_WINDOW", self.__dialog.destroy)

        self.__dialog.geometry("+%d+%d" % (self.__root.winfo_width()/3,
                                          self.__root.winfo_height()/3))

    def get_config_and_destroy(self):
        try:
            configs = {}
            for config in self.__configs:
                config.value = config.get_entry_val()
                configs[config.label] = config
        except:    
            self.__dialog.destroy()
            raise IException("Illegal values, please try again")
        
        self.__dialog.destroy()
        
        return configs

        
    def quit(self):
        if tkMessageBox.askokcancel("Quit", "Do you want to quit?"):
            self.__root.quit()
            self.__root.destroy()
        

    def debug(self, event):
        print("DEBUG")
        

    def __apply(self, f, *args):
        try:
            f(*args)
        except IException as exc:
            self.__log_exception( str(exc), True )
        except:
            self.__log_exception( IException.format_exception() )
                
    
    def __log_exception(self, message, warning = False):
        if warning == True:
            self.show_warning( message )
        elif IException.DEBUG == True:
            self.append_logger( message )
        else:
            self.show_error( message )