gui.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import logging
  2. import queue
  3. import signal
  4. import tkinter as tk
  5. from tkinter import filedialog, messagebox, HORIZONTAL, VERTICAL, ttk
  6. from gui.splash import Splash
  7. from gui.forms import FormUi
  8. from gui.console import ConsoleUi
  9. from gui.status import StatusUi
  10. from qhandler import QueueHandler
  11. logger = logging.getLogger('spr')
  12. def show_error(msg):
  13. messagebox.showerror(
  14. "Error del Sistema", msg)
  15. sys.exit()
  16. def get_file():
  17. file = filedialog.askopenfilename(
  18. filetypes=[('Excel', '.xlsx')], title='Seleccione un archivo de subasta...')
  19. if not file:
  20. messagebox.showerror(
  21. "Error - Selección de Archivo", "Debe seleccionar un archivo para ejecutar la subasta!!")
  22. sys.exit()
  23. def center(win):
  24. """
  25. centers a tkinter window
  26. :param win: the root or Toplevel window to center
  27. """
  28. win.update_idletasks()
  29. width = win.winfo_width()
  30. frm_width = win.winfo_rootx() - win.winfo_x()
  31. win_width = width + 2 * frm_width
  32. height = win.winfo_height()
  33. titlebar_height = win.winfo_rooty() - win.winfo_y()
  34. win_height = height + titlebar_height + frm_width
  35. x = win.winfo_screenwidth() // 2 - win_width // 2
  36. y = win.winfo_screenheight() // 2 - win_height // 2
  37. win.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  38. win.deiconify()
  39. class App:
  40. def __init__(self, root):
  41. self.root = root
  42. Splash(self.root)
  43. # Queue a nivel de App para manejar el progress bar
  44. pbqueue = queue.Queue()
  45. log_queue = queue.Queue()
  46. root.title('Simulación Predespacho del MER')
  47. root.iconbitmap("app.ico")
  48. self.queue_handler = QueueHandler(log_queue)
  49. formatter = logging.Formatter(
  50. '%(asctime)s: %(message)s', datefmt='%Y-%m-%dT%H:%M:%S')
  51. self.queue_handler.setFormatter(formatter)
  52. # self.queue_handler.setLevel(logging.INFO)
  53. logger.addHandler(self.queue_handler)
  54. w = ttk.Notebook(self.root)
  55. w.grid(row=1, column=0, sticky="nsew",
  56. padx=10, pady=(0, 10))
  57. tab_exec = ttk.Frame(w)
  58. # Create the panes and frames
  59. vertical_pane = ttk.PanedWindow(
  60. tab_exec, orient=VERTICAL)
  61. vertical_pane.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)
  62. main_title = ttk.Label(
  63. self.root,
  64. text='Simulación del Predespacho del MER',
  65. font=('', '20', 'bold'))
  66. main_title.grid(row=0, column=0, pady=10)
  67. horizontal_pane = ttk.PanedWindow(vertical_pane, orient=HORIZONTAL)
  68. vertical_pane.add(horizontal_pane, weight=1)
  69. form_frame = ttk.Labelframe(
  70. horizontal_pane, text="Configuración", padding=(5, 5))
  71. form_frame.columnconfigure(1, weight=1)
  72. horizontal_pane.add(form_frame, weight=1)
  73. console_frame = ttk.Labelframe(
  74. horizontal_pane, text="Consola", padding=(5, 5))
  75. console_frame.columnconfigure(0, weight=1)
  76. console_frame.rowconfigure(0, weight=1)
  77. horizontal_pane.add(console_frame, weight=1)
  78. status_frame = ttk.Labelframe(
  79. vertical_pane, text="Estado", padding=(5, 5))
  80. vertical_pane.add(status_frame, weight=0)
  81. # Initialize all frames
  82. self.form = FormUi(form_frame, pbqueue)
  83. self.console = ConsoleUi(console_frame, log_queue, self.queue_handler)
  84. self.status = StatusUi(status_frame, pbqueue)
  85. w.add(tab_exec, text='Ejecución')
  86. # bindings
  87. self.root.protocol('WM_DELETE_WINDOW', self.quit)
  88. self.root.bind('<Control-q>', self.quit)
  89. signal.signal(signal.SIGINT, self.quit)
  90. center(self.root)
  91. def quit(self, *args):
  92. msg = 'Se esta ejecutando un proceso de optimización\n\n'
  93. msg += '¿Desea salir de la aplicación?'
  94. if self.form.runspr_thread and self.form.runspr_thread.is_alive():
  95. if messagebox.askokcancel("Salir de la Simulación", msg):
  96. logger.info('Cerrando aplicacion...')
  97. self.form.runspr_thread.stop()
  98. self.root.destroy()
  99. else:
  100. self.root.destroy()