from tkinter import ttk
from tkinter import *
from tkinter import Tk, Frame, Menu
import tkinter as tk
import sqlite3
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("База данных")
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
fileeMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="Фамилия")
submenu.add_command(label="Имя")
submenu.add_command(label="Школа")
submenu.add_command(label="Год рождения")
fileMenu.add_cascade(label='Сортировка', menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Запуск", underline=0, command=self.View2)
fileeMenu.add_separator()
fileeMenu.add_command(label="Выход", underline=0, command=self.onExit)
menubar.add_cascade(label="Файл", underline=0, menu=fileMenu)
menubar.add_cascade(label="Окно", underline=0, menu=fileeMenu)
def onExit(self):
self.quit()
def View2(self):
sub = tk.Toplevel(root)
sub.transient(root)
sub.title('Таблица базы данных')
sub.geometry("940x240")
tree = ttk.Treeview(sub, style="mystyle.Treeview",
column=("column1", "column2", "column3", "column4", "column5", "column6", "column7"),
show='headings')
tree.place()
tree.heading("#1", text="Код")
tree.column("#1", minwidth=0, width=35)
tree.heading("#2", text="Фамилия")
tree.heading("#3", text="Имя")
tree.heading("#4", text="Отчество", )
tree.heading("#5", text="Год")
tree.column("#5", minwidth=0, width=90)
tree.heading("#6", text="Школа")
tree.column("#6", minwidth=0, width=50)
tree.heading("#7", text="Класс")
tree.column("#7", minwidth=0, width=50)
tree.pack()
conn = sqlite3.connect("mydatabase.db")
cur = conn.cursor()
cur.execute("SELECT * FROM spisok")
rows = cur.fetchall()
for row in rows:
print(row) # it print all records in the database
tree.insert("", tk.END, values=row)
conn.close()
def connect():
conn = sqlite3.connect("mydatabase.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, First TEXT, Surname TEXT)")
conn.commit()
def Delete():
conn = sqlite3.connect("mydatabase.db")
cur = conn.cursor()
t = editor.get()
cur.execute('DELETE FROM spisok WHERE name = ?', (t,))
conn.commit()
cur.close()
conn.close()
def Delete2():
conn = sqlite3.connect("mydatabase.db")
cur = conn.cursor()
t = editor8.get()
cur.execute('DELETE FROM spisok WHERE fam = ?', (t,))
conn.commit()
cur.close()
conn.close()
def Delete3():
conn = sqlite3.connect("mydatabase.db")
cur = conn.cursor()
t = editor9.get()
cur.execute('DELETE FROM spisok WHERE Id = ?', (t,))
conn.commit()
cur.close()
conn.close()
def Add():
conn = sqlite3.connect("mydatabase.db")
cur = conn.cursor()
id = editor1.get()
fam = editor2.get()
name = editor3.get()
deadname = editor4.get()
year = editor5.get()
school = editor6.get()
klass = editor7.get()
# Вставляем данные в таблицу
cur.execute('''INSERT INTO spisok(id, fam, name, deadname, year, school, klass) VALUES (?, ?, ?, ?, ?, ?, ?)''', (id, fam, name, deadname, year, school, klass))
conn.commit()
# Сохраняем изменения
conn.commit()
cur.close()
conn.close()
connect() # this to create the db
root = tk.Tk()
root.geometry("680x300")
editor=Entry(text="imea",state=NORMAL)
editor.place(x=5,y=110,width=100)
editor8=Entry(text="fam",state=NORMAL)
editor8.place(x=235,y=110,width=100)
editor9=Entry(text="kod",state=NORMAL)
editor9.place(x=5,y=170,width=50)
editor1=Entry(text="код",state=NORMAL)
editor1.place(x=0,y=50,width=35)
editor2=Entry(text="фамилия",state=NORMAL)
editor2.place(x=40,y=50,width=100)
editor3=Entry(text="имя",state=NORMAL)
editor3.place(x=145,y=50,width=100)
editor4=Entry(text="отчество",state=NORMAL)
editor4.place(x=250,y=50,width=100)
editor5=Entry(text="Год",state=NORMAL)
editor5.place(x=355,y=50,width=50)
editor6=Entry(text="Школа",state=NORMAL)
editor6.place(x=410,y=50,width=50)
editor7=Entry(text="Класс",state=NORMAL)
editor7.place(x=470,y=50,width=50)
b3 = tk.Button(text="Добавить", command=Add)
b3.place(x=550,y=45)
#Надписи
label1 = Label(text="Добавить новую строку в базу данных")
label1.grid(row=0, column=0,ipadx=0, ipady=0, padx=1, pady=1, sticky="w")
label2 = Label(text="Код Фамилия Имя Отчество Год Школа Класс")
label2.grid(row=1, column=0,ipadx=0, ipady=0, padx=0, pady=0, sticky="w")
label3 = Label(text="Удалить по имени")
label3.place(x=0,y=90)
label4 = Label(text="Удалить по фамилии")
label4.place(x=225,y=90)
label5 = Label(text="Удалить по коду")
label5.place(x=0,y=150)
b2 = tk.Button(text="Удалить", command=Delete)
b2.place(x=110,y=105)
b3 = tk.Button(text="Удалить", command=Delete2)
b3.place(x=350,y=105)
b4 = tk.Button(text="Удалить", command=Delete3)
b4.place(x=80,y=170)
app = Example()
root.mainloop()