You are on page 1of 2

import tkinter as tk

from tkinter.filedialog import askopenfilename, asksaveasfilename


import os

def open_file():
filepath = askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if not filepath:
return
txt_edit.delete(1.0, tk.END)
with open(filepath, "r") as input_file:
text = input_file.read()
txt_edit.insert(tk.END, text)
window.title(f"Текстовый редактор - {filepath}")

def save_file():
filepath = asksaveasfilename(
defaultextension="txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
)
if not filepath:
return
with open(filepath, "w") as output_file:
text = txt_edit.get(1.0, tk.END)
output_file.write(text)
window.title(f"Текстовый редактор - {filepath}")

def toggle_theme():
if window.cget("background") == "#F0F8FF":
window.configure(background="#333333")
fr_buttons.configure(background="#333333")
txt_edit.configure(background="#333333", fg="#F0F8FF")
btn_open.configure(background="#F0F8FF", fg="#333333")
btn_save.configure(background="#F0F8FF", fg="#333333")
btn_toggle_theme.configure(text="Light Theme")
else:
window.configure(background="#F0F8FF")
fr_buttons.configure(background="#fff")
txt_edit.configure(background="#F0F8FF", fg="#333333")
btn_open.configure(background="#F0F8FF", fg="#333333")
btn_save.configure(background="#F0F8FF", fg="#333333")
btn_toggle_theme.configure(text="Dark Theme")

window = tk.Tk()
window.configure(background='#F0F8FF')
window.title("Текстовый редактор")
window.rowconfigure(0, minsize=800, weight=1)
window.columnconfigure(1, minsize=800, weight=1)

txt_edit = tk.Text(window,background='#F0F8FF')
fr_buttons = tk.Frame(window, relief=tk.RAISED, bd=2,background='#fff')
btn_open = tk.Button(fr_buttons, text="Открыть",
background='#F0F8FF',command=open_file)
btn_save = tk.Button(fr_buttons, text="Сохранить как...",background='#F0F8FF',
command=save_file)
btn_toggle_theme = tk.Button(fr_buttons, text="Dark Theme", background="#F0F8FF",
fg="#333333", command=toggle_theme)
btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
btn_save.grid(row=1, column=0, sticky="ew", padx=5)
btn_toggle_theme.grid(row=2, column=0, sticky="ew", padx=5, pady=5)

fr_buttons.grid(row=0, column=0, sticky="ns")


txt_edit.grid(row=0, column=1, sticky="nsew")

window.mainloop()

You might also like