99 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
	
		
		
			
		
	
	
			99 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
	
| 
								 | 
							
								import tkinter as tk
							 | 
						||
| 
								 | 
							
								from tkinter import filedialog, messagebox
							 | 
						||
| 
								 | 
							
								import os
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class DIZEditorApp:
							 | 
						||
| 
								 | 
							
								    def __init__(self, root):
							 | 
						||
| 
								 | 
							
								        self.root = root
							 | 
						||
| 
								 | 
							
								        self.root.title("DIZ Editor")
							 | 
						||
| 
								 | 
							
								        self.root.geometry("600x400")
							 | 
						||
| 
								 | 
							
								        self.filepath = None
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        # Fuente monoespaciada
							 | 
						||
| 
								 | 
							
								        self.monospace_font = ("Courier", 10)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        # Text editing frame
							 | 
						||
| 
								 | 
							
								        self.text_frame = tk.Frame(self.root)
							 | 
						||
| 
								 | 
							
								        self.text_frame.pack(fill=tk.BOTH, expand=True)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        self.text_vars = []
							 | 
						||
| 
								 | 
							
								        self.text_entries = []
							 | 
						||
| 
								 | 
							
								        self.char_counts = []
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        for i in range(10):
							 | 
						||
| 
								 | 
							
								            frame = tk.Frame(self.text_frame)
							 | 
						||
| 
								 | 
							
								            frame.pack(fill=tk.X)
							 | 
						||
| 
								 | 
							
								            
							 | 
						||
| 
								 | 
							
								            text_var = tk.StringVar()
							 | 
						||
| 
								 | 
							
								            text_entry = tk.Entry(frame, textvariable=text_var, width=45, font=self.monospace_font)
							 | 
						||
| 
								 | 
							
								            text_entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
							 | 
						||
| 
								 | 
							
								            
							 | 
						||
| 
								 | 
							
								            char_count = tk.Label(frame, text="0/45")
							 | 
						||
| 
								 | 
							
								            char_count.pack(side=tk.RIGHT)
							 | 
						||
| 
								 | 
							
								            
							 | 
						||
| 
								 | 
							
								            text_var.trace_add('write', self.update_char_count(text_var, char_count))
							 | 
						||
| 
								 | 
							
								            
							 | 
						||
| 
								 | 
							
								            self.text_vars.append(text_var)
							 | 
						||
| 
								 | 
							
								            self.text_entries.append(text_entry)
							 | 
						||
| 
								 | 
							
								            self.char_counts.append(char_count)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        # File explorer frame
							 | 
						||
| 
								 | 
							
								        self.file_frame = tk.Frame(self.root)
							 | 
						||
| 
								 | 
							
								        self.file_frame.pack(fill=tk.X)
							 | 
						||
| 
								 | 
							
								        self.file_label = tk.Label(self.file_frame, text="No file selected")
							 | 
						||
| 
								 | 
							
								        self.file_label.pack(side=tk.LEFT)
							 | 
						||
| 
								 | 
							
								        self.open_button = tk.Button(self.file_frame, text="Open", command=self.open_file)
							 | 
						||
| 
								 | 
							
								        self.open_button.pack(side=tk.LEFT)
							 | 
						||
| 
								 | 
							
								        self.save_button = tk.Button(self.file_frame, text="Save", command=self.save_file)
							 | 
						||
| 
								 | 
							
								        self.save_button.pack(side=tk.LEFT)
							 | 
						||
| 
								 | 
							
								        self.save_as_button = tk.Button(self.file_frame, text="Save As", command=self.save_as_file)
							 | 
						||
| 
								 | 
							
								        self.save_as_button.pack(side=tk.LEFT)
							 | 
						||
| 
								 | 
							
								        self.exit_button = tk.Button(self.file_frame, text="Exit", command=self.exit_app)
							 | 
						||
| 
								 | 
							
								        self.exit_button.pack(side=tk.RIGHT)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    def update_char_count(self, text_var, char_count):
							 | 
						||
| 
								 | 
							
								        def callback(*args):
							 | 
						||
| 
								 | 
							
								            text = text_var.get()
							 | 
						||
| 
								 | 
							
								            if len(text) > 45:
							 | 
						||
| 
								 | 
							
								                text_var.set(text[:45])
							 | 
						||
| 
								 | 
							
								            char_count.config(text=f"{len(text)}/45")
							 | 
						||
| 
								 | 
							
								        return callback
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    def open_file(self):
							 | 
						||
| 
								 | 
							
								        filepath = filedialog.askopenfilename(filetypes=[("DIZ files", "*.diz")])
							 | 
						||
| 
								 | 
							
								        if filepath:
							 | 
						||
| 
								 | 
							
								            with open(filepath, 'r') as file:
							 | 
						||
| 
								 | 
							
								                lines = file.readlines()
							 | 
						||
| 
								 | 
							
								                for i in range(min(10, len(lines))):
							 | 
						||
| 
								 | 
							
								                    self.text_vars[i].set(lines[i].strip())
							 | 
						||
| 
								 | 
							
								                for i in range(len(lines), 10):
							 | 
						||
| 
								 | 
							
								                    self.text_vars[i].set('')
							 | 
						||
| 
								 | 
							
								            self.filepath = filepath
							 | 
						||
| 
								 | 
							
								            self.file_label.config(text=os.path.basename(filepath))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    def save_file(self):
							 | 
						||
| 
								 | 
							
								        if self.filepath:
							 | 
						||
| 
								 | 
							
								            self.write_to_file(self.filepath)
							 | 
						||
| 
								 | 
							
								        else:
							 | 
						||
| 
								 | 
							
								            self.save_as_file()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    def save_as_file(self):
							 | 
						||
| 
								 | 
							
								        filepath = filedialog.asksaveasfilename(defaultextension=".diz", filetypes=[("DIZ files", "*.diz")])
							 | 
						||
| 
								 | 
							
								        if filepath:
							 | 
						||
| 
								 | 
							
								            self.write_to_file(filepath)
							 | 
						||
| 
								 | 
							
								            self.filepath = filepath
							 | 
						||
| 
								 | 
							
								            self.file_label.config(text=os.path.basename(filepath))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    def write_to_file(self, filepath):
							 | 
						||
| 
								 | 
							
								        with open(filepath, 'w') as file:
							 | 
						||
| 
								 | 
							
								            for text_var in self.text_vars:
							 | 
						||
| 
								 | 
							
								                file.write(text_var.get() + '\n')
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    def exit_app(self):
							 | 
						||
| 
								 | 
							
								        self.root.quit()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								if __name__ == "__main__":
							 | 
						||
| 
								 | 
							
								    root = tk.Tk()
							 | 
						||
| 
								 | 
							
								    app = DIZEditorApp(root)
							 | 
						||
| 
								 | 
							
								    root.mainloop()
							 |