import tkinter as tk
from tkinter import messagebox
FILE_NAME = "quotes.txt"
def read_file():
try:
with open(FILE_NAME, "r", encoding="utf-8") as file:
return file.read()
except FileNotFoundError:
return ""
def add_quote():
quote = quote_entry.get().strip()
author = author_entry.get().strip()
if not quote or not author:
messagebox.showwarning(
"Ошибка", "Введите цитату и автора!"
)
return
with open(FILE_NAME, "a", encoding="utf-8") as file:
file.write(quote + "\n")
file.write("(" + author + ")\n\n")
quote_entry.delete(0, tk.END)
author_entry.delete(0, tk.END)
update_text()
messagebox.showinfo("Готово", "Цитата добавлена!")
def update_text():
text_area.delete("1.0", tk.END)
text_area.insert(tk.END, read_file())
def show_authors():
authors = []
for line in read_file().splitlines():
line = line.strip()
if line.startswith("(") and line.endswith(")"):
authors.append(line)
messagebox.showinfo(
"Список авторов",
"\n".join(authors) if authors else "Авторов пока нет"
)
root = tk.Tk()
root.title("Цитаты и авторы")
root.geometry("600x550")
tk.Label(
root, text="Содержимое файла quotes.txt",
font=("Arial", 14, "bold")
).pack(pady=5)
text_area = tk.Text(root, height=12, width=65)
text_area.pack(padx=10, pady=5)
tk.Label(root, text="Введите цитату:").pack()
quote_entry = tk.Entry(root, width=65)
quote_entry.pack(padx=10, pady=5)
tk.Label(root, text="Введите автора:").pack()
author_entry = tk.Entry(root, width=65)
author_entry.pack(padx=10, pady=5)
tk.Button(
root, text="Добавить цитату",
command=add_quote
).pack(pady=8)
tk.Button(
root, text="Показать всех авторов",
command=show_authors
).pack(pady=5)
tk.Button(
root, text="Обновить файл",
command=update_text
).pack(pady=5)
update_text()
root.mainloop()