init commit

This commit is contained in:
vadzik 2023-12-08 14:34:09 +03:00
commit 699697d0b6
5 changed files with 166 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
venv
__pycache__

2
cfg.ini Normal file
View File

@ -0,0 +1,2 @@
[DEFAULT]
db_path = db.json

1
db.json Normal file
View File

@ -0,0 +1 @@
[]

46
db/__init__.py Normal file
View File

@ -0,0 +1,46 @@
from typing import List
from pydantic import BaseModel, Field, TypeAdapter
from uuid import UUID, uuid4
import json
class Record(BaseModel):
id: UUID = Field(default_factory=uuid4)
name: str | None = ''
description:str | None = ''
img_path: str | None = ''
class DB:
records: List[Record] = []
path = ''
def __init__(self, path=''):
if path != '':
with open(path, 'r') as file:
js_data = json.load(file)
tmp = []
for item in js_data:
tmp.append(json.loads(item))
js_data = tmp
self.records = [Record(**model_data) for model_data in js_data]
self.path = path
def add_record(self, name='', description='', img_path=''):
new_rec = Record(name=name, description=description, img_path=img_path)
self.records.append(new_rec)
with open(self.path, 'w') as file:
model_list_json = [record.model_dump_json() for record in self.records]
json.dump(model_list_json, file)
def edit_record(self, id):
pass
def delete_record(self, id):
for i in range(len(self.records)):
if self.records[i].id == id:
self.records.pop(i)

115
main.py Normal file
View File

@ -0,0 +1,115 @@
from tkinter import *
import configparser
from tkinter.ttk import Combobox
import db
root = None
database = None
config = None
listbox = None
def load_config():
global config
config = configparser.ConfigParser()
config.read('cfg.ini')
print(config['DEFAULT']['db_path'])
def load_database():
global database, config
database = db.DB(config['DEFAULT']['db_path'])
def help_menu():
global root
newWindow = Toplevel(root)
newWindow.title("New Window")
newWindow.geometry("200x200")
Label(newWindow,
text ="This is a new window").pack()
def fond_menu():
global root
newWindow = Toplevel(root)
newWindow.title("New Window")
newWindow.geometry("200x200")
Label(newWindow,
text ="This is a new window").pack()
def close_prog(event):
exit()
def key_pressed(event):
print(event.keysym)
if event.keysym == 'F1':
print("Клавиша 'F1' была нажата")
if event.keysym == 'F2':
print("Клавиша 'F2' была нажата")
if event.keysym == 'F3':
print("Клавиша 'F3' была нажата")
if event.keysym == 'F4':
print("Клавиша 'F4' была нажата")
def selected(event):
global listbox
print(event)
# получаем индексы выделенных элементов
selected_indices = listbox.curselection()
# получаем сами выделенные элементы
selected_langs = ",".join([listbox.get(i) for i in selected_indices])
msg = f"вы выбрали: {selected_langs}"
print(msg)
def main():
global root, listbox, database
load_config()
load_database()
root = Tk()
root.title("amDB")
root.geometry('600x400+50+50')
root.minsize(400, 300)
root.bind('<KeyPress-F1>', key_pressed)
root.bind('<KeyPress-F2>', key_pressed)
root.bind('<KeyPress-F3>', key_pressed)
root.bind('<KeyPress-F4>', key_pressed)
root.bind('<KeyPress-F10>', key_pressed)
root.bind('<Control-x>', close_prog)
main_menu = Menu()
fond_menu = Menu(tearoff=0)
fond_menu.add_command(label="Найти...")
fond_menu.add_separator()
fond_menu.add_command(label="Добавить")
fond_menu.add_command(label="Удалить")
fond_menu.add_command(label="Изменить")
fond_menu.add_separator()
fond_menu.add_command(label="Выход")
help_menu = Menu(tearoff=0)
help_menu.add_command(label="Содержание")
help_menu.add_separator()
help_menu.add_command(label="О программе")
main_menu.add_cascade(label="Фонд", menu=fond_menu)
main_menu.add_cascade(label="Справка", menu=help_menu)
label = Label(root, text="F1-справка F2-добавить F3-удалить F4-изменить F10-меню", bd=1, relief=SUNKEN, anchor=W)
label.pack(side=BOTTOM, fill=X)
listbox = Listbox(root, justify="left", height=200)
listbox.bind("<<ListboxSelect>>", selected)
listbox.pack(side='top', anchor='nw', fill=Y)
for record in database.records:
listbox.insert(record.id, record.name)
root.config(menu=main_menu)
root.mainloop()
if __name__ == "__main__":
main()