from tkinter import *
from child_window import ChildWindow
from tkinter import messagebox
from PIL import Image as pilImage
from PIL import ImageTk
import os
class Window:
def __init__(self, width, height, title="MyWindow", resizable=(False, False), icon=r"icon/gerb.ico"):
self.root = Tk()
self.root.title(title)
self.root.geometry("540x125+200+200")
self.root.resizable(resizable[0], resizable[1])
if icon:
self.root.iconbitmap(icon)
img = pilImage.open(r"icon/personal.png")
img = img.resize((100, 100), pilImage.ANTIALIAS)
self.photo_image = ImageTk.PhotoImage(img)
img1 = pilImage.open(r"icon/podriydchik.png")
img = img1.resize((100, 100), pilImage.ANTIALIAS)
self.photo_image1 = ImageTk.PhotoImage(img)
img2 = pilImage.open(r"icon/spravochnik.png")
img = img2.resize((100, 100), pilImage.ANTIALIAS)
self.photo_image2 = ImageTk.PhotoImage(img)
img3 = pilImage.open(r"icon/info.png")
img = img3.resize((100, 100), pilImage.ANTIALIAS)
self.photo_image3 = ImageTk.PhotoImage(img)
img4 = pilImage.open(r"icon/exit.png")
img = img4.resize((100, 100), pilImage.ANTIALIAS)
self.photo_image4 = ImageTk.PhotoImage(img)
def run(self):
self.draw_widgets()
self.root.mainloop()
def draw_widgets(self):
Button(self.root, text="Персонал", image=self.photo_image, compound=TOP, command=self.button_action).grid(row=0, column=0)
Button(self.root, text="Подрядчики", image=self.photo_image1, compound=TOP, command=self.button_action1).grid(row=0, column=1)
Button(self.root, text="Справочник", image=self.photo_image2, compound=TOP, command=self.button_action2).grid(row=0, column=2)
Button(self.root, text="Инфо", image=self.photo_image3, compound=TOP, command=self.finding).grid(row=0, column=3)
Button(self.root, text="Выход", image=self.photo_image4, compound=TOP, command=self.exit).grid(row=0, column=4)
def button_action(self):
os.startfile("C:/Users/Валерий/Desktop/Doc/Список ФИО.docx")
def button_action1(self):
os.startfile("C:/Users/Валерий/Desktop/Doc/Подрядчики.docx")
def button_action2(self):
os.startfile("C:/Users/Валерий/Desktop/Doc/Телефонный Справочник.doc")
def finding(self):
messagebox.showinfo("info", "")
def exit(self):
choice = messagebox.askyesno("Выход", "Вы действительно хотите выйте?")
if choice:
self.root.destroy()
def create_child(self, width, height, title="Child", resizable=(False, False), icon=None):
ChildWindow(self.root, width, height, title, resizable, icon)
if __name__ == "__main__":
window = Window(540, 125, "")
window.create_child(550, 230, "Войдите в систему.")
window.run()
Дочернее окно
from tkinter import *
class ChildWindow:
def __init__(self, parent, width, height, title="MyWindow", resizable=(False, False), icon=None):
self.root = Toplevel(parent)
self.root.title(title)
self.root.geometry(f"{width}x{height}+200+200")
self.root.resizable(resizable[0], resizable[1])
if icon:
self.root.iconbitmap(icon)
self.grad_focus()
def grad_focus(self):
self.root.grab_set()
self.root.focus_set()
self.root.wait_window()
Вот код который не могу вписать в дочернее окно
from tkinter import *
from tkinter import messagebox as ms
import sqlite3
with sqlite3.connect('quit.db') as db:
cursor = db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS user (username TEXT NOT NULL PRIMARY KEY,password TEX NOT NULL);')
db.commit()
db.close()
class main:
def __init__(self, master):
self.master = master
self.username = StringVar()
self.password = StringVar()
self.n_username = StringVar()
self.n_password = StringVar()
self.widgets()
def login(self):
with sqlite3.connect('quit.db') as db:
cursor = db.cursor()
find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
cursor.execute(find_user, [(self.username.get()), (self.password.get())])
result = cursor.fetchall()
if result:
self.logf.pack_forget()
self.head['text'] = self.username.get() + '\n Добро пожаловать!'
self.head['pady'] = 150
else:
ms.showerror('Авторизация!', 'В базу данных такой аккаунт не записан!.')
def new_user(self):
with sqlite3.connect('quit.db') as db:
cursor = db.cursor()
find_user = ('SELECT username FROM user WHERE username = ?')
cursor.execute(find_user, [(self.n_username.get())])
if cursor.fetchall():
ms.showerror('Предупреждение!', 'Введите имя и пароль!')
else:
ms.showinfo('Регистрация!', 'Вы зарегистрировались!')
self.log()
insert = 'INSERT INTO user(username,password) VALUES(?,?)'
cursor.execute(insert, [(self.n_username.get()), (self.n_password.get())])
db.commit()
def log(self):
self.username.set('')
self.password.set('')
self.crf.pack_forget()
self.head['text'] = 'LOGIN'
self.logf.pack()
def cr(self):
self.n_username.set('')
self.n_password.set('')
self.logf.pack_forget()
self.head['text'] = 'Реристрация'
self.crf.pack()
def widgets(self):
self.head = Label(self.master, text='Автризация', font=('', 35), pady=10)
self.head.pack()
self.logf = Frame(self.master, padx=10, pady=10)
Label(self.logf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.logf, textvariable=self.username, bd=5, font=('', 15)).grid(row=0, column=1)
Label(self.logf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.logf, textvariable=self.password, bd=5, font=('', 15), show='*').grid(row=1, column=1)
Button(self.logf, text=' Войти ', bd=3, font=('', 15), padx=5, pady=5, command=self.login).grid()
Button(self.logf, text=' Регистрация ', bd=3, font=('', 15), padx=5, pady=5, command=self.cr).grid(row=2,
column=1)
self.logf.pack()
self.crf = Frame(self.master, padx=10, pady=10)
Label(self.crf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_username, bd=5, font=('', 15)).grid(row=0, column=1)
Label(self.crf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_password, bd=5, font=('', 15), show='*').grid(row=1, column=1)
Button(self.crf, text='Зарегистрироваться', bd=3, font=('', 15), padx=5, pady=5, command=self.new_user).grid()
Button(self.crf, text='Назад', bd=3, font=('', 15), padx=5, pady=5, command=self.log).grid(row=2,
column=1)
if __name__ == '__main__':
root = Tk()
root.title('Войдите в систему')
main(root)
root.mainloop()