/
Inqell
/
Random-Quote-Generator
Обзор
Документация
Войти
/
Inqell
/
Random-Quote-Generator
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.py
127 строк
3 KB
Inqell
create: main.py, new_file, update: README.md
03 май 2026, 20:50
Верифицирован
03 май 2026, 20:50
903b53d
Код
Авторство
О чём код?
import tkinter as tk from tkinter import ttk, messagebox import random import json HISTORY_FILE = "history_quotes.json" # Предопределённые цитаты quotes = [ {"text": "Stay hungry, stay foolish.", "author": "Steve Jobs", "theme": "Motivation"}, {"text": "To be or not to be.", "author": "Shakespeare", "theme": "Philosophy"}, {"text": "Knowledge is power.", "author": "Francis Bacon", "theme": "Education"}, {"text": "Time is money.", "author": "Benjamin Franklin", "theme": "Life"} ] # Загрузка истории def load_history(): try: with open(HISTORY_FILE, "r") as f: return json.load(f) except: return [] # Сохранение def save_history(): with open(HISTORY_FILE, "w") as f: json.dump(history, f, indent=4) history = load_history() # Обновление списка истории def update_list(): listbox.delete(0, tk.END) for item in history: listbox.insert(tk.END, f'{item["text"]} — {item["author"]} [{item["theme"]}]') # Генерация цитаты def generate_quote(): filtered = quotes author = combo_author.get() theme = combo_theme.get() if author != "All": filtered = [q for q in filtered if q["author"] == author] if theme != "All": filtered = [q for q in filtered if q["theme"] == theme] if not filtered: messagebox.showinfo("Нет результатов", "Нет цитат по фильтру") return quote = random.choice(filtered) label_text.config(text=quote["text"]) label_author.config(text=f'{quote["author"]} ({quote["theme"]})') history.append(quote) save_history() update_list() # Добавление новой цитаты def add_quote(): text = entry_text.get() author = entry_author.get() theme = entry_theme.get() if not text or not author or not theme: messagebox.showerror("Ошибка", "Заполните все поля") return new_quote = {"text": text, "author": author, "theme": theme} quotes.append(new_quote) messagebox.showinfo("Успех", "Цитата добавлена") # GUI root = tk.Tk() root.title("Random Quote Generator") root.geometry("600x500") # Отображение цитаты label_text = tk.Label(root, text="", wraplength=500, font=("Arial", 14)) label_text.pack(pady=10) label_author = tk.Label(root, text="", font=("Arial", 10)) label_author.pack() # Фильтры authors = ["All"] + list(set(q["author"] for q in quotes)) themes = ["All"] + list(set(q["theme"] for q in quotes)) combo_author = ttk.Combobox(root, values=authors) combo_author.set("All") combo_author.pack() combo_theme = ttk.Combobox(root, values=themes) combo_theme.set("All") combo_theme.pack() # Кнопка btn = tk.Button(root, text="Сгенерировать цитату", command=generate_quote) btn.pack(pady=10) # История listbox = tk.Listbox(root, height=10) listbox.pack(fill="both", expand=True) # Добавление цитаты tk.Label(root, text="Добавить цитату").pack() entry_text = tk.Entry(root) entry_text.pack() entry_author = tk.Entry(root) entry_author.pack() entry_theme = tk.Entry(root) entry_theme.pack() btn_add = tk.Button(root, text="Добавить", command=add_quote) btn_add.pack() update_list() root.mainloop()