From cea3e027ad4f70d4420e3a81bcb438537e9bebbf Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Thu, 27 Oct 2022 00:30:22 -0500 Subject: Se añade interfaz gráfica MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 88 +++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 30 deletions(-) diff --git a/main.py b/main.py index f14a2be..1df4337 100644 --- a/main.py +++ b/main.py @@ -1,38 +1,66 @@ import sys, getopt +from tkinter import * +from tkinter import ttk, filedialog from lexer import * -def inicio (input_file, output_file): - with open(input_file) as f: - data = f.read() - inicio_lexer (data) - -def main(argv): - try: - opts, args = getopt.getopt(argv[1:], "hi:o:", ["input=", "output="]) - except getopt.GetoptError as err: - print(err) - print_help(argv[0]); - sys.exit(2) - +class Main: input_file = None output_file = None + text = None + + def abrir_archivo(self): + self.input_file = filedialog.askopenfilename() + with open(self.input_file) as f: + data = f.read() + self.text.insert(END, data) + + def guardar_archivo(self): + data = self.text.get('1.0', 'end-1c') + with open(self.input_file, "r+") as f: + f.truncate(0) + f.write(data) + + def compilar_programa(self): + self.output_file = filedialog.asksaveasfilename() + data = self.text.get('1.0', 'end-1c') + inicio_lexer(data) + + def ejecutar_programa(self): + print('ejecutar_programa()') + + def salir(self): + exit(0) + + def main_gui(self, argv): + root = Tk() + root.title ("Javañol") + + mainframe = ttk.Frame(root, padding="3 3 12 12") + mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) + + # Menú + win = Toplevel(root) - for o, a in opts: - if o == "-h": - print_help (argv[0]) - elif o in ("-i", "--input"): - input_file = a - elif o in ("-o", "--output"): - output_file = a - else: - assert False, "opción desconocida" - - if input_file and output_file: - inicio (input_file, output_file) - -def print_help (arg0): - print("Uso: % s -i entrada.ñ -o salida.ñ" % arg0) - print(" % s -h" % arg0) + menubar = Menu(win) + + menu_file = Menu(menubar) + menubar.add_cascade(menu=menu_file, label='Archivo') + menu_file.add_command(label='Abrir', command=self.abrir_archivo) + menu_file.add_command(label='Guardar', command=self.guardar_archivo) + menu_file.add_command(label='Salir', command=self.salir) + + menu_program = Menu(menubar) + menubar.add_cascade(menu=menu_program, label='Programa') + menu_program.add_command(label='Compilar', command=self.compilar_programa) + menu_program.add_command(label='Ejecutar', command=self.ejecutar_programa) + + win['menu'] = menubar + + self.text = Text(mainframe) + self.text.grid(column=0, row=0, sticky=(N, W, E, S)) + + root.mainloop() if __name__ == "__main__": - main(sys.argv) + main = Main() + main.main_gui(sys.argv) -- cgit v1.2.3