# Set default font
self.font_family = "Arial" # Starting with a printed font
self.font_size = 12
# Create a Notebook (tabbed interface)
self.notebook = ttk.Notebook(self.root)
self.notebook.pack(expand=True, fill=tk.BOTH)
self.notebook.bind("<<NotebookTabChanged>>", self.on_tab_changed) # Bind tab change event
# Create a menu
self.create_menu()
def create_menu(self):
menu_bar = Menu(self.root)
# File menu
file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New Tab", command=self.new_file)
file_menu.add_command(label="Open", command=self.open_file)
file_menu.add_command(label="Save", command=self.save_file)
file_menu.add_command(label="Save As", command=self.save_as)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.exit_app)
menu_bar.add_cascade(label="File", menu=file_menu)
# Edit menu
edit_menu = Menu(menu_bar, tearoff=0)
edit_menu.add_command(label="Undo", command=self.undo_action)
edit_menu.add_command(label="Redo", command=self.redo_action)
edit_menu.add_separator()
edit_menu.add_command(label="Cut", command=self.cut_text)
edit_menu.add_command(label="Copy", command=self.copy_text)
edit_menu.add_command(label="Paste", command=self.paste_text)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
# Format menu
format_menu = Menu(menu_bar, tearoff=0)
format_menu.add_command(label="Normal", command=self.set_normal)
format_menu.add_command(label="Bold", command=self.toggle_bold)
format_menu.add_command(label="Italic", command=self.toggle_italic)
format_menu.add_command(label="Text Color", command=self.change_text_color)
format_menu.add_separator()
format_menu.add_command(label="Printed Font", command=self.set_printed_font)
format_menu.add_command(label="Lowercase", command=self.convert_to_lowercase)
format_menu.add_command(label="Upper Case", command=self.convert_to_uppercase)
menu_bar.add_cascade(label="Format", menu=format_menu)
# Configure the menu
self.root.config(menu=menu_bar)
def new_file(self):
frame = ttk.Frame(self.notebook)
self.notebook.add(frame, text="Untitled")
text_area = tk.Text(frame, wrap=tk.WORD, bg="white", fg="black", insertbackground="black", undo=True)
text_area.pack(expand=True, fill=tk.BOTH)
# Create and display a close button (X) on the right side
close_button = tk.Button(frame, text="X", command=lambda tab_id=self.notebook.tabs()[1]: self.close_tab(tab_id)) # Pass tab ID
close_button.pack(side=tk.RIGHT)
text_area.config(font=tkFont.Font(family=self.font_family, size=self.font_size))
def open_file(self):
# ... rest of the code for opening files ...
# ... rest of the code for other functions ...
def close_tab(self, tab_id):
self.notebook.forget(tab_id)
def on_tab_changed(self, event):
# Update the font and other settings when a tab is selected
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
text_area.config(font=tkFont.Font(family=self.font_family, size=self.font_size))
if name == "main":
root = tk.Tk()
editor = TextEditor(root)
root.mainloop()
scrie codul complet
Sure, I'll provide a complete implementation of the TextEditor class, filling in the various methods to ensure the application is fully functional. This includes file opening and saving, as well as text editing features. Note that I will also include proper handling for some of the interactions needed in a basic text editor.
python
import tkinter as tk
from tkinter import filedialog, colorchooser, Menu, font as tkFont
from tkinter import ttk
class TextEditor:
def __init__(self, root):
self.root = root
self.root.title("bogdan Notepad")
self.root.geometry("800x600")
# Set default font
self.font_family = "Arial" # Starting with a printed font
self.font_size = 12
# Create a Notebook (tabbed interface)
self.notebook = ttk.Notebook(self.root)
self.notebook.pack(expand=True, fill=tk.BOTH)
self.notebook.bind("<<NotebookTabChanged>>", self.on_tab_changed)
# Create a menu
self.create_menu()
# Add a first tab by default
self.new_file()
def create_menu(self):
menu_bar = Menu(self.root)
# File menu
file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New Tab", command=self.new_file)
file_menu.add_command(label="Open", command=self.open_file)
file_menu.add_command(label="Save", command=self.save_file)
file_menu.add_command(label="Save As", command=self.save_as)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.exit_app)
menu_bar.add_cascade(label="File", menu=file_menu)
# Edit menu
edit_menu = Menu(menu_bar, tearoff=0)
edit_menu.add_command(label="Undo", command=self.undo_action)
edit_menu.add_command(label="Redo", command=self.redo_action)
edit_menu.add_separator()
edit_menu.add_command(label="Cut", command=self.cut_text)
edit_menu.add_command(label="Copy", command=self.copy_text)
edit_menu.add_command(label="Paste", command=self.paste_text)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
# Format menu
format_menu = Menu(menu_bar, tearoff=0)
format_menu.add_command(label="Normal", command=self.set_normal)
format_menu.add_command(label="Bold", command=self.toggle_bold)
format_menu.add_command(label="Italic", command=self.toggle_italic)
format_menu.add_command(label="Text Color", command=self.change_text_color)
format_menu.add_separator()
format_menu.add_command(label="Printed Font", command=self.set_printed_font)
format_menu.add_command(label="Lowercase", command=self.convert_to_lowercase)
format_menu.add_command(label="Upper Case", command=self.convert_to_uppercase)
menu_bar.add_cascade(label="Format", menu=format_menu)
# Configure the menu
self.root.config(menu=menu_bar)
def new_file(self):
frame = ttk.Frame(self.notebook)
self.notebook.add(frame, text="Untitled")
text_area = tk.Text(frame, wrap=tk.WORD, bg="white", fg="black", insertbackground="black", undo=True)
text_area.pack(expand=True, fill=tk.BOTH)
# Save reference to keep track of text content
text_area.bind("<<Modified>>", self.on_text_modified)
text_area.config(font=tkFont.Font(family=self.font_family, size=self.font_size))
def open_file(self):
file_path = filedialog.askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if file_path:
with open(file_path, "r") as file:
content = file.read()
self.new_file()
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
text_area.insert(tk.END, content)
self.notebook.tab(current_tab, text=file_path.split("/")[-1]) # Set tab title
def save_file(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area and 'file_path' in text_area.tags:
file_path = text_area.tags['file_path']
if file_path:
with open(file_path, "w") as file:
file.write(text_area.get(1.0, tk.END).strip())
else:
self.save_as()
def save_as(self):
file_path = filedialog.asksaveasfilename(
defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if file_path:
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
with open(file_path, "w") as file:
file.write(text_area.get(1.0, tk.END).strip())
self.notebook.tab(current_tab, text=file_path.split("/")[-1])
text_area.tags['file_path'] = file_path
def exit_app(self):
self.root.quit()
def undo_action(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
try:
text_area.edit_undo()
except tk.TclError:
pass
def redo_action(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
try:
text_area.edit_redo()
except tk.TclError:
pass
def cut_text(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
text_area.event_generate("<<Cut>>")
def copy_text(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
text_area.event_generate("<<Copy>>")
def paste_text(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
text_area.event_generate("<<Paste>>")
def set_normal(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
text_area.config(font=tkFont.Font(family=self.font_family, size=self.font_size))
def toggle_bold(self):
self.font_family = "Arial Bold" if self.font_family != "Arial Bold" else "Arial"
self.apply_font()
def toggle_italic(self):
self.font_family = "Arial Italic" if self.font_family != "Arial Italic" else "Arial"
self.apply_font()
def change_text_color(self):
color = colorchooser.askcolor()[1]
if color:
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
text_area.config(fg=color)
def set_printed_font(self):
self.font_family = "Courier"
self.apply_font()
def convert_to_lowercase(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
content = text_area.get(1.0, tk.END).lower()
text_area.delete(1.0, tk.END)
text_area.insert(tk.END, content)
def convert_to_uppercase(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
content = text_area.get(1.0, tk.END).upper()
text_area.delete(1.0, tk.END)
text_area.insert(tk.END, content)
def apply_font(self):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
text_area.config(font=tkFont.Font(family=self.font_family, size=self.font_size))
def get_current_text_widget(self, tab_widget):
for widget in self.notebook.nametowidget(tab_widget).winfo_children():
if isinstance(widget, tk.Text):
return widget
return None
def close_tab(self, tab_id):
self.notebook.forget(tab_id)
def on_tab_changed(self, event):
current_tab = self.notebook.select()
text_area = self.get_current_text_widget(current_tab)
if text_area:
text_area.config(font=tkFont.Font(family=self.font_family, size=self.font_size))
def on_text_modified(self, event):
text_area = event.widget
text_area.edit_modified(False)
if __name__ == "__main__":
root = tk.Tk()
editor = TextEditor(root)
root.mainloop()
This code builds a functional text editor with multiple tabs, allowing you to open and save files, switch between tabs, and use basic text manipulation features. Adjustments can always be made to enhance functionality, including adding more features or refining existing ones.