Загрузка данных



import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3

# Создание базы данных
db = sqlite3.connect("library.db")
cursor = db.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS books (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL,
    author TEXT NOT NULL,
    year INTEGER,
    available INTEGER DEFAULT 1
)
""")
db.commit()

# Создание окна
root = tk.Tk()
root.title("Библиотека")
root.geometry("750x500")

# Поля ввода
tk.Label(root, text="Название книги").pack()
title_entry = tk.Entry(root)
title_entry.pack()

tk.Label(root, text="Автор").pack()
author_entry = tk.Entry(root)
author_entry.pack()

tk.Label(root, text="Год издания").pack()
year_entry = tk.Entry(root)
year_entry.pack()

# Таблица Treeview
tree = ttk.Treeview(
    root,
    columns=("id", "title", "author", "year", "available"),
    show="headings"
)

tree.heading("id", text="ID")
tree.heading("title", text="Название")
tree.heading("author", text="Автор")
tree.heading("year", text="Год")
tree.heading("available", text="Доступность")

tree.pack(fill="both", expand=True, pady=10)


# Добавление книги
def add_book():
    title = title_entry.get()
    author = author_entry.get()
    year = year_entry.get()

    if not title or not author or not year:
        messagebox.showerror("Ошибка", "Заполните все поля")
        return

    try:
        year = int(year)
    except ValueError:
        messagebox.showerror("Ошибка", "Год должен быть числом")
        return

    cursor.execute(
        "INSERT INTO books (title, author, year, available) VALUES (?, ?, ?, 1)",
        (title, author, year)
    )

    db.commit()
    show_books()

    title_entry.delete(0, tk.END)
    author_entry.delete(0, tk.END)
    year_entry.delete(0, tk.END)


# Показать все книги
def show_books():
    for item in tree.get_children():
        tree.delete(item)

    cursor.execute("SELECT * FROM books")
    rows = cursor.fetchall()

    for row in rows:
        tree.insert("", "end", values=row)


# Поиск книг автора
def search_author():
    author = author_entry.get()

    for item in tree.get_children():
        tree.delete(item)

    cursor.execute(
        "SELECT * FROM books WHERE author LIKE ?",
        ('%' + author + '%',)
    )

    rows = cursor.fetchall()

    for row in rows:
        tree.insert("", "end", values=row)


# Отметить книгу как выданную
def issue_book():
    selected = tree.selection()

    if not selected:
        messagebox.showwarning("Внимание", "Выберите книгу")
        return

    book_id = tree.item(selected[0], "values")[0]

    cursor.execute(
        "UPDATE books SET available = 0 WHERE id = ?",
        (book_id,)
    )

    db.commit()
    show_books()


# Кнопки
tk.Button(root, text="Добавить книгу",
          command=add_book).pack(pady=3)

tk.Button(root, text="Показать все книги",
          command=show_books).pack(pady=3)

tk.Button(root, text="Найти книги автора",
          command=search_author).pack(pady=3)

tk.Button(root, text="Выдать книгу",
          command=issue_book).pack(pady=3)

# Первоначальное отображение
show_books()

root.mainloop()

db.close()