from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import registry from sqlalchemy import create_engine from sqlalchemy.sql.expression import and_ from datetime import date from models import * from tkinter import * from functools import partial from tkinter import messagebox, ttk mapper_registry = registry() engine = create_engine('sqlite:///dipl.db', echo=True) Base = mapper_registry.generate_base() Session = sessionmaker(bind=engine) def main(): window = Tk() window.geometry("400x400") window.title("Вход в аккаунт") window.config(bg="#5D6D7E") lbl1=Label(window,text="Добро пожаловать",font=("",25),fg="#fed9b7",bg="#5D6D7E") lbl1.pack(anchor="center") lbl2=Label(window,text="Логин:",font=("",20),fg="#fdfcdc",bg="#5D6D7E") lbl2.place(x=10,y=90) ent1=Entry(window) ent1.place(x=200,y=100) lbl3=Label(window,text="Пароль:",font=("",20),fg="#fdfcdc",bg="#5D6D7E") lbl3.place(x=10,y=170) ent3=Entry(window) ent3.place(x=200,y=180) btn1=Button(window,text="Принять", command=lambda: validateLogin(ent1.get(), ent3.get())) btn1.place(x=250,y=300) btn2=Button(window,text="Настройки",command=options) btn2.place(x=70,y=300) window.mainloop() def options(): window = Tk() window.geometry("400x400") window.title("Настройки") window.config(bg="#5D6D7E") lbl1=Label(window,text="Выбирите таблицу",font=("",25),fg="#fed9b7",bg="#5D6D7E") lbl1.pack(anchor="center") bo1=Button(window,text="1. Добавить должность",command=addDolgnostGui) bo1.place(x=50,y=100) bo2=Button(window,text="2. Добавить пользователя",command=addUserGui) bo2.place(x=200,y=100) bo3=Button(window,text="3. Добавить принтер",command=addPrinterGui) bo3.place(x=50,y=200) bo4=Button(window,text="4. Добавить пластик",command=addPlasticGui) bo4.place(x=200,y=200) window.mainloop() def validateLogin(username, password): sess = Session() users = sess.query(Users).filter(Users.login==username, Users.passwd==password) results = users.first() if hasattr(results,"login"): messagebox.showinfo(title="Успех", message="Вы успешно авторизировались в ситеме.") else: messagebox.showinfo(title="Успех", message="Вы НЕ авторизировались в ситеме.") return def addUserGui(): window = Tk() window.geometry("300x500") window.config(bg="#5D6D7E") l1 = Label(window, text="ФИО") l1.place(x=50,y=100) fio = Entry(window) fio.place(x=150,y=100) l2 = Label(window, text="Должность") l2.place(x=50,y=150) dlg = getDolgnost() dlg_var = StringVar(value=dlg[0]) dolgnost = ttk.Combobox(window,textvariable=dlg_var, values=dlg) dolgnost.place(x=150,y=150) l3 = Label(window, text="Логин") l3.place(x=50,y=250) login = Entry(window) login.place(x=150,y=250) l4 = Label(window, text="Пароль") l4.place(x=50,y=300) passwd = Entry(window) passwd.place(x=150,y=300) print(dolgnost.get()) button_add = Button(window, text="Добавить", command=lambda: addUser(fio.get(), dolgnost.get(), login.get(), passwd.get())) button_add.place(x=100,y=350) def addUser(fio, dolgnost, login, passwd): sess = Session() dlg = sess.query(Dolgnost).filter(Dolgnost.dolgnst==dolgnost).first() newuser = Users(fio=fio, id_dolgnost=dlg.id, login=login, passwd=passwd) sess.add(newuser) sess.commit() sess.close() window.mainloop() def addPrinterGui(): window = Tk() window.geometry("300x300") window.config(bg="#5D6D7E") lp1 = Label(window, text="Название") lp1.place(x=15,y=20) nazvaniee = Entry(window) nazvaniee.place(x=160,y=20) lp2 = Label(window, text="Макс Скорость Печати") lp2.place(x=15,y=70) maxSpedPrint = Entry(window) maxSpedPrint.place(x=160,y=70) lp3 = Label(window, text="Мин Скорость Печати") lp3.place(x=15,y=120) minSpedPrint = Entry(window) minSpedPrint.place(x=160,y=120) bapg=Button(window,text="Добавить",command=lambda:addPrinter(nazvaniee.get(), maxSpedPrint.get(), minSpedPrint.get())) bapg.place(x=220,y=170) def addPrinter(nazvanie, maxsped,minspeed): sess = Session() new = Printer(nazvanie=nazvanie, maxSpedPrint=maxsped, minSpedPrint=minspeed) sess.add(new) sess.commit() sess.close() minSpedPrint.delete(0, END) maxSpedPrint.delete(0, END) nazvaniee.delete(0, END) window.mainloop() def addPlasticGui(): window = Tk() window.geometry("380x350") window.config(bg="#5D6D7E") lpl1 = Label(window, text="Название") lpl1.place(x=15,y=20) nazvanie = Entry(window) nazvanie.place(x=170,y=20) lpl2 = Label(window, text="Диаметр") lpl2.place(x=15,y=70) diametr = Entry(window) diametr.place(x=170,y=70) lpl3 = Label(window, text="Мин Температура Печати") lpl3.place(x=15,y=120) mintempprint = Entry(window) mintempprint.place(x=170,y=120) lpl4 = Label(window, text="Макс Температура Печати") lpl4.place(x=15,y=170) maxtemprint = Entry(window) maxtemprint.place(x=170,y=170) lpl5 = Label(window, text="Дата Производства") lpl5.place(x=15,y=230) days = list(range(1, 32)) months = list(range(1, 13)) years = list(range(1940, 2026)) # Create ComboBox for days day_combobox = ttk.Combobox(window,values=days,width=2) day_combobox.place(x=140,y=230) # Create ComboBox for months month_combobox = ttk.Combobox(window,values=months,width=2) month_combobox.place(x=185,y=230) # Create ComboBox for years year_combobox = ttk.Combobox(window,values=years,width=4) year_combobox.place(x=225,y=230) def convert_to_date(): # Retrieve selected values from the ComboBoxes selected_day = int(day_combobox.get()) selected_month = int(month_combobox.get()) selected_year = int(year_combobox.get()) # Convert the selected values to a date object date_obj = date(selected_year, selected_month, selected_day) return date_obj bapg=Button(window,text="Добавить",command=lambda:addPlastic(nazvanie.get(), diametr.get(), mintempprint.get(), maxtemprint.get(), convert_to_date())) bapg.place(x=230,y=280) def addPlastic(nazvanie, diametr, mintempprint, maxtempprint, dateproizv): sess = Session() new = Plastic(nazvanie=nazvanie, diametr=diametr, minTempPrint=mintempprint, maxTempPrint=maxtempprint, dateproizv=dateproizv) sess.add(new) sess.commit() sess.close() window.mainloop def addDolgnostGui(): window = Tk() window.geometry("300x120") window.config(bg="#5D6D7E") l1 = Label(window,bg="#5D6D7E",text="Должность") l1.grid(row=1, column=0) dolg = Entry(window) dolg.grid(row=1, column=1) button_add = Button(window, text="Добавить", command=lambda: addDolgnost(dolg.get())) button_add.grid(row=5, column=2) def addDolgnost(dolgnost): sess = Session() new = Dolgnost(dolgnst = dolgnost) sess.add(new) sess.commit() sess.close() dolg.delete(0, END) window.mainloop() def addPrintSettingsGui(): window = Tk() window.geometry("800x600") prntc = getPinters() prnt_var = StringVar(value = prntc[0]) plstc = getPlastics() plst_var = StringVar(value=plstc[0]) l1 = Label(window,bg="#5D6D7E",text="Выберите принтер") l1.place(x=10, y=30) prnt = ttk.Combobox(window,textvariable=prnt_var, values=prntc) prnt.place(x=210,y=20) l2 = Label(window,bg="#5D6D7E",text="Выберите пластик") l2.place(x=10, y=80) plst = ttk.Combobox(window,textvariable=plst_var, values=plstc) plst.place(x=210,y=80) l3 = Label(window,bg="#5D6D7E",text="Введите температуру сопла") l3.place(x=10, y=120) tempsopla = Entry(window) tempsopla.place(x=210, y=120) l4 = Label(window,bg="#5D6D7E",text="Введите температуру стола") l4.place(x=10, y=200) tempstola = Entry(window) tempstola.place(x=210, y=200) l5 = Label(window,bg="#5D6D7E",text="Введите процент заполнения") l5.place(x=10, y=250) procentZapoln= Entry(window) procentZapoln.place(x=210, y=250) l6 = Label(window,bg="#5D6D7E",text="Введите скорость печати") l6.place(x=10, y=250) printSpeed= Entry(window) printSpeed.place(x=210, y=300) goodset = IntVar() chk = Checkbutton(window, text='Оптимальные настройки', variable=goodset) chk.place(x=10, y=350) l6 = Label(window,bg="#5D6D7E",text="Примечание") l6.place(x=10, y=400) primech= Entry(window) primech.place(x=210, y=400) button_add = Button(window, text="Добавить", command=lambda: addPrintSettings(prnt.get(), plst.get(), tempsopla.get(), tempstola.get(), procentZapoln.get(), goodset.get(), printSpeed.get(), primech.get())) button_add.place(x=100, y=400) def addPrintSettings(prnt, plst, tempstola, tempsopla, procentzapoln, goodset, printspeed, primech): sess = Session() printer = sess.query(Printer).filter(Printer.nazvanie==prnt).first() plast = sess.query(Plastic).filter(Plastic.nazvanie==plst).first() gs=False if goodset == 0: gs=False else: gs=True import time addtime=time.localtime() new = PrintSettings(tempStol=tempstola, tempSopl=tempsopla, procentZapoln=procentzapoln, PrintSpeed=printspeed, good_set=gs, #time_add=addtime, primechanie=primech, id_printer=printer.id, id_plastic=plast.id, ) sess.add(new) sess.commit() sess.close() #minSpedPrint.delete(0, END) """ dataFrame = Frame(window) dataFrame.pack() table_pre_data = {'tempstol':'Темп. стола', 'tempsopl':'Темп. сопла'} table_columns = [] table_headers = [] for key, value in table_pre_data.items(): table_columns.append(key) table_headers.append(value) prDataFrame = ttk.Treeview(dataFrame) prDataFrame['columns'] = table_columns for col in table_columns: prDataFrame.column(col, width=100) for col in table_headers: prDataFrame.heading(col,text=value) #bapg=Button(window,text="Добавить",command=lambda:getPrintSetings()) """ window.mainloop() def getPrintSettings(): sess = Session() print() print("query") print() """ psettings = (sess.query(PrintSettings, Printer, Plastic) .join(Printer, PrintSettings.id_printer==Printer.id) .filter(Printer.id==1) .join(Plastic, PrintSettings.id_plastic==Plastic.id) .filter(Plastic.nazvanie=="fdplast") .all() ) """ psettings= ( sess.query(PrintSettings).join(Printer).join(Plastic) .filter((Printer.nazvanie=="ender3" and Plastic.nazvanie=="fdplast" ) ).all() ) print(psettings) for qwe in psettings: print(qwe.id_plastic) plst_naz = sess.query(Plastic).filter(Plastic.id==psettings[0].id_plastic).first() print() print(plst_naz.nazvanie) print() #print(psettings[0][1].nazvanie) print() sess.close() def getUsers(): sess= Session() users = sess.query(Users).join(Dolgnost).all() for user in users: dolg = sess.query(Dolgnost).filter(Dolgnost.id==user.id_dolgnost).first() print(user.fio) print(dolg.dolgnst) def getDolgnost(): sess = Session() dlggs = sess.query(Dolgnost).all() print(dlggs) dlg_list = [] for dlg in dlggs: dlg_list.append(dlg.dolgnst) sess.close() return dlg_list def getPinters(): sess = Session() printers = sess.query(Printer).all() prnt = [] for printer in printers: prnt.append(printer.nazvanie) sess.close() return prnt def getPlastics(): sess = Session() plastics = sess.query(Plastic).all() plst = [] for plastic in plastics: plst.append(plastic.nazvanie) sess.close() return plst if __name__ == '__main__': addDolgnostGui()