Commit 56c1db36 authored by Alessandro dos Santos Ferreira's avatar Alessandro dos Santos Ferreira
Browse files

PEP 8 -- Style Guide for Python Code

parent 5baa60fe
__all__ = ["Interface", "TkInterface"]
\ No newline at end of file
from interface import Interface, InterfaceException
from tk_interface import TkInterface
__all__ = ["interface",
"tk_interface"]
\ No newline at end of file
......@@ -2,11 +2,12 @@
# -*- coding: utf-8 -*-
#
"""
Nome: Interface.py
Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Descricão: Classe abstrata para os modulos de interface.
Abstract class for graphical interface.
Name: interface.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import traceback
from abc import ABCMeta, abstractmethod
......
__all__ = ["TkCanvas", "TkConfig", "TkCustomDialog", "TkCustomFrame", "TkLogger", "TkMenu", "TkItemMenu", "TkUtils"]
\ No newline at end of file
from tk_canvas import Image
from tk_config import TkConfig
from tk_customdialog import SimpleDialog, ConfigDialog, ChooseOneDialog
from tk_customframe import CustomGrid
from tk_logger import Log
from tk_menu import Menu
from tk_itemmenu import ItemMenu, Command, Separator, CheckButton
from tk_utils import Utils
__all__ = ["tk_canvas",
"tk_config",
"tk_customdialog",
"tk_customframe",
"tk_logger",
"tk_menu",
"tk_itemmenu",
"tk_utils"]
......@@ -2,10 +2,10 @@
# -*- coding: utf-8 -*-
#
"""
Nome: TkCanvas.py
Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Descricão: TODO
Provides structured graphics interface from Tkinter package.
Name: tk_canvas.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import sys
......@@ -22,10 +22,6 @@ from matplotlib import pylab as plt
class Image(object):
parent = None
__im = None
__canvas = None
def __init__(self, parent, image, onclick = None):
self.parent = parent
......@@ -33,36 +29,36 @@ class Image(object):
self.parent.image = image
fig = plt.figure(facecolor='white', edgecolor='black', linewidth=1)
self.__im = plt.imshow(self.parent.image) # later use a.set_data(new_data)
self._im = plt.imshow(self.parent.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.parent)
self.__canvas.show()
self.__canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self._canvas = FigureCanvasTkAgg(fig, master=self.parent)
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 = onclick)
def refresh(self, image):
if self.__canvas is not None:
if self._canvas is not None:
self.parent.image = image
self.__im.set_data(self.parent.image)
self.__canvas.draw()
self._im.set_data(self.parent.image)
self._canvas.draw()
def close(self):
if self.__canvas is not None:
if self._canvas is not None:
self.parent.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.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
self._im = None
self._canvas = None
return True
return False
......@@ -2,20 +2,19 @@
# -*- coding: utf-8 -*-
#
"""
Nome: TkConfig.py
Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Descricão: TODO
Extends util.config.Config with tk_entry field.
Name: tk_config.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
from util.Config import Config
from util.config import Config
class TkConfig(Config):
tk_entry = None
def __init__(self, label, value, c_type, tk_entry = None, hidden = False, min_value = None, max_value = None):
super(self.__class__, self).__init__(label, value, c_type, hidden, min_value, max_value)
def __init__(self, label, value, c_type, tk_entry = None, hidden = False, meta = None):
super(self.__class__, self).__init__(label, value, c_type, hidden, meta)
self.tk_entry = tk_entry
def get_entry_val(self):
......
......@@ -2,11 +2,12 @@
# -*- coding: utf-8 -*-
#
"""
Nome: TkCustomDialog.py
Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Descricão: Classe que customiza as janelas de dialogo da biblioteca TkInter.
Provides customized dialogs extending Tkinter.Toplevel.
Name: tk_customdialog.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
......@@ -15,12 +16,11 @@ else:
from collections import OrderedDict
from TkConfig import TkConfig
from tk_config import TkConfig
class SimpleDialog(Tk.Toplevel):
parent = None
def __init__(self, parent, title = None, command_ok = None):
self.parent = parent
......@@ -40,18 +40,18 @@ class SimpleDialog(Tk.Toplevel):
class ConfigDialog(SimpleDialog):
__configs = None
def __init__(self, parent, title = None, configs = None, command_ok = None):
SimpleDialog.__init__(self, parent, title, command_ok)
self._configs = None
if configs is not None:
self.add_configs(configs, command_ok)
def add_configs(self, configs, command_ok):
row = 0
self.__configs = OrderedDict()
self._configs = OrderedDict()
for key in configs:
if configs[key].hidden == False:
Tk.Label(self, text=configs[key].label).grid(row=row, padx=4, pady=4, sticky=Tk.W)
......@@ -63,9 +63,9 @@ class ConfigDialog(SimpleDialog):
entry.focus_set()
row += 1
self.__configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, tk_entry=entry, hidden=configs[key].hidden)
self._configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, tk_entry=entry, hidden=configs[key].hidden, meta=configs[key].meta )
else:
self.__configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, hidden=configs[key].hidden )
self._configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, hidden=configs[key].hidden, meta=configs[key].meta )
B1 = Tk.Button(self, text="Ok", width=5, command = command_ok)
B1.grid(row=row, padx=6, pady=6, sticky=Tk.W)
......@@ -79,8 +79,59 @@ class ConfigDialog(SimpleDialog):
def update_and_validate_configs(self):
for key in self.__configs:
self.__configs[key].value = self.__configs[key].get_entry_val()
for key in self._configs:
self._configs[key].value = self._configs[key].get_entry_val()
def get_configs(self):
return self._configs
class ChooseOneDialog(SimpleDialog):
def __init__(self, parent, title = None, configs = None, command_ok = None):
SimpleDialog.__init__(self, parent, title, command_ok)
self.v = Tk.StringVar()
self._configs = None
if configs is not None:
self.add_configs(configs, command_ok)
def add_configs(self, configs, command_ok):
row = 0
self._configs = OrderedDict()
for key in configs:
if configs[key].hidden == False:
Tk.Label(self, text=configs[key].label).grid(row=row, padx=4, pady=4, sticky=Tk.W)
radio = Tk.Radiobutton(self, text=configs[key].label, value=key, variable=self.v, width=25,
bg='white', fg='black', padx=4, pady=4, indicatoron=1, anchor=Tk.W,
activebackground='white', activeforeground='black', highlightbackground='white')
radio.grid(row=row, padx=4, pady=4, sticky=Tk.W)
if configs[key].value == True:
radio.select()
row += 1
self._configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, tk_entry=radio, hidden=configs[key].hidden, meta=configs[key].meta)
else:
self._configs[key] = TkConfig(configs[key].label, configs[key].value, c_type=configs[key].c_type, hidden=configs[key].hidden, meta=configs[key].meta )
B1 = Tk.Button(self, text="Ok", width=5, command = command_ok)
B1.grid(row=row, padx=6, pady=6, sticky=Tk.W+Tk.E)
self.bind("<Return>", command_ok)
#B2 = Tk.Button(self, text="Cancel", width=5, command = self.destroy)
#B2.grid(row=row+1, column=0, padx=6, pady=6, sticky=Tk.W)
self.bind("<Escape>", lambda *_: self.destroy())
def update_and_validate_configs(self):
for key in self._configs:
self._configs[key].value = False
self._configs[self.v.get()].value = True
def get_configs(self):
return self.__configs
return self._configs
......@@ -2,25 +2,23 @@
# -*- coding: utf-8 -*-
#
"""
Nome: TkCustomFrame.py
Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Descricão: TODO.
Provides customized grids extending Tkinter.Frame.
Name: tk_customframe.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
from TkUtils import Utils
from util.X11Colors import Colors
from tk_utils import Utils
from util.x11_colors import X11Colors
class CustomGrid(Tk.Frame):
parent = None
v = None
def __init__(self, parent, width = 0, height = 0, bg='white'):
self.parent = parent
......@@ -30,18 +28,21 @@ class CustomGrid(Tk.Frame):
Tk.Frame.__init__(self, self.parent, width=width, height=height, bg=bg)
def add_cell_label(self, text, row, column, width=0, height=0, bg='white', fg="black"):
Tk.Label(self, text=text, width=width, height=height, bg=bg, fg=fg, padx=4, pady=4).grid(row=row, column=column)
def add_cell_button_color(self, text, row, column, width=0, height=0, bg='white', fg="black", command=None, command_args=None):
bg_color = Colors.get_color_hex(bg)
bg_color = X11Colors.get_color_hex(bg)
bt = Tk.Button(self, text=text, width=width, height=height, bg=bg_color, fg=fg, padx=0, pady=0, cursor="hand1",
command=lambda *_: command(command_args))
bt.grid(row=row, column=column)
def add_cell_radio_button(self, text, value, row, column, width=0, height=0, bg='white', fg="black", selected=False, command=None, command_args=None):
radio = Tk.Radiobutton(self, text=text, variable=self.v, value=value,
......
......@@ -2,10 +2,10 @@
# -*- coding: utf-8 -*-
#
"""
Nome: ItemMenu.py
Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Descricão: TODO
Implements all items from menu inheriting from abstract class ItemMenu.
Name: main.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import sys
if sys.version_info[0] < 3:
......
......@@ -2,10 +2,10 @@
# -*- coding: utf-8 -*-
#
"""
Nome: Logger.py
Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Descricão: TODO
Creates a log on bottom of window using a Tkinter.Text.
Name: tk_logger.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import sys
......@@ -14,18 +14,15 @@ if sys.version_info[0] < 3:
else:
import tkinter as Tk
class Logger(object):
parent = None
class Log(object):
__body = None
def __init__(self, parent):
self.parent = parent
self.__body = Tk.Text(self.parent, width=0, height=10, bg="white", fg="black", padx=5, pady=5)
self.__body.insert(Tk.INSERT, "$ Pynovisao is ready to use!\n")
self.__body.config(state=Tk.DISABLED)
self.__body.pack(side=Tk.BOTTOM, fill=Tk.X, expand=False)
self._body = Tk.Text(self.parent, width=0, height=10, bg="white", fg="black", padx=5, pady=5)
self._body.insert(Tk.INSERT, "$ Pynovisao is ready to use!\n")
self._body.config(state=Tk.DISABLED)
self._body.pack(side=Tk.BOTTOM, fill=Tk.X, expand=False)
def write_logger(self, fmt, *args):
......@@ -33,17 +30,17 @@ class Logger(object):
self.append_logger(fmt % args)
def append_logger(self, fmt, *args):
self.__body.config(state=Tk.NORMAL)
self.__body.insert(Tk.END, fmt % args)
self.__body.insert(Tk.END, '\n')
self.__body.config(state=Tk.DISABLED)
self._body.config(state=Tk.NORMAL)
self._body.insert(Tk.END, fmt % args)
self._body.insert(Tk.END, '\n')
self._body.config(state=Tk.DISABLED)
def clear_logger(self):
self.__body.config(state=Tk.NORMAL)
self.__body.delete('1.0', Tk.END)
self.__body.insert('1.0', '$ ')
self.__body.config(state=Tk.DISABLED)
self._body.config(state=Tk.NORMAL)
self._body.delete('1.0', Tk.END)
self._body.insert('1.0', '$ ')
self._body.config(state=Tk.DISABLED)
def destroy_logger(self):
self.__body.pack_forget();
self.__body.destroy();
self._body.pack_forget();
self._body.destroy();
......@@ -2,10 +2,10 @@
# -*- coding: utf-8 -*-
#
"""
Nome: Menu.py
Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Descricão: TODO
Provides menu structure functionalities.
Name: tk_menu.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import sys
......@@ -14,33 +14,33 @@ if sys.version_info[0] < 3:
else:
import tkinter as Tk
from TkItemMenu import *
from tk_itemmenu import *
class Menu(object):
parent = None
label = None
__items_menu = None
_items_menu = None
def __init__(self, parent, label):
self.parent = parent
self.label = label
self.__items_menu = []
self._items_menu = []
def add_command(self, label, action, shortcut):
self.__items_menu.append( Command(self, label, action, shortcut) )
self._items_menu.append( Command(self, label, action, shortcut) )
def add_separator(self):
self.__items_menu.append( Separator(self) )
self._items_menu.append( Separator(self) )
def add_check_button(self, label, action, shortcut):
self.__items_menu.append( CheckButton(self, label, action, shortcut) )
self._items_menu.append( CheckButton(self, label, action, shortcut) )
def render(self, menubar):
menu = Tk.Menu(menubar, tearoff=0)
for item in self.__items_menu:
for item in self._items_menu:
item.render(menu)
if item.shortcut is not None:
......
......@@ -2,10 +2,10 @@
# -*- coding: utf-8 -*-
#
"""
Nome: TkUtils.py
Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Descricão: TODO
Provides static method utilities from Tkinter package.
Name: tk_utils.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import os
import tkMessageBox, Tkconstants, tkFileDialog
......
......@@ -2,11 +2,12 @@
# -*- 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.
Implements graphical interface using Tkinter package modules.
Name: main.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
......@@ -14,138 +15,135 @@ else:
import tkinter as Tk
import tkMessageBox
from Interface import Interface, InterfaceException as IException
from tk import *
import tk as tk_local
from interface import Interface, InterfaceException as IException
class TkInterface(Interface):
MAX_CLASSES = 20
utils = TkUtils.Utils
title = None
__root = None
__menus = None
__image = None
__grid = None
__logger = None
utils = tk_local.Utils
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._root = Tk.Tk()
self._root.wm_title(self.title)
self._root.geometry('%dx%d+%d+%d' % (800, 600, 0, 0))
self.__menus = []
self._menus = []
self._image = None
self._grid = None
self._logger = None
self._conf_dialog = None
def set_subtitle(self, subtitle):
self.__root.wm_title(self.title + ' - ' + subtitle)
self._root.wm_title(self.title + ' - ' + subtitle)
def add_menu(self, label):
self.__menus.append( TkMenu.Menu(self.__root, label) )
self._menus.append( tk_local.Menu(self._root, label) )
def add_command(self, label, action, shortcut = None):
self.__menus[-1].add_command( label = label, action = lambda *_: self.__apply( action ), shortcut = shortcut )
self._menus[-1].add_command( label = label, action = lambda *_: self._apply( action ), shortcut = shortcut )
def add_separator(self):
self.__menus[-1].add_separator( )
self._menus[-1].add_separator( )
def add_check_button(self, label, action, shortcut = None):
self.__menus[-1].add_check_button(label = label, action = lambda *_: self.__apply( action ), shortcut = shortcut)
self._menus[-1].add_check_button(label = label, action = lambda *_: self._apply( action ), shortcut = shortcut)
def render_menu(self):
menubar = Tk.Menu(self.__root)
menubar = Tk.Menu(self._root)
for menu in self.__menus:
for menu in self._menus:
menu.render(menubar)
self.__root.config(menu=menubar)
self._root.config(menu=menubar)
def add_image(self, image, title = None, onclick = None):
if self.__image is not None:
self.__image.close()
if self._image is not None:
self._image.close()
self.__image = TkCanvas.Image(self.__root, image, lambda event, *_: self.__apply( onclick, event ))
self._image = tk_local.Image(self._root, image, lambda event, *_: self._apply( onclick, event ))
if title is not None:
self.set_subtitle(title)
def refresh_image(self, image, title = None):
if self.__image is not None:
self.__image.refresh(image)
if self._image is not None:
self._image.refresh(image)