Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from itertools import groupby
- try:
- class Person:
- instances = []
- data_from_entries = ''
- search = ''
- save = ''
- def __init__(self, name, surname, sex, age, number, location):
- self.name = name
- self.surname = surname
- self.sex = sex
- self.age = age
- self.number = number
- self.location = location
- self.instances.append(self)
- @staticmethod
- def create_file():
- file = open('objects.txt', 'w')
- file.close()
- @staticmethod
- def create_removed():
- file = open('removed.txt','w')
- file.close()
- @staticmethod
- def output(a, b):
- inp = Person.search
- a.config(text=Person.search)
- test = False
- pass_name = set()
- persons = ''
- for i in Person.instances:
- if i.surname.strip() == inp.strip():
- test = True
- if i.name not in pass_name:
- persons += f'имя: {i.name}\nпол: {i.sex}\nвозраст: {i.age} \nномер: {i.number} \nадрес: {i.location}\n_____________________________________________________\n'
- pass_name.add(i.name)
- if test:
- b.config(text=persons)
- else:
- b.config(text='Фамилия не найдена')
- @staticmethod
- def append_obj_to_file():
- with open('objects.txt', 'a+', encoding='utf-8') as file:
- data = Person.data_from_entries
- # print(data)
- s = file.read()
- if data.count('не указано') < 6:
- file.write(data + '|')
- file.seek(0)
- @staticmethod
- def test_file_for_valid_format(n):
- def test_string_has_right_format(x):
- return not bool(len(x.split('|')[:-1]) % 6) and '|' in x
- with open(n, encoding='utf-8') as file:
- s = file.read()
- if s:
- if s[-1] != '|':
- s += '|'
- if test_string_has_right_format(s):
- return s
- if s[-1] != '|':
- s += '|'
- new_n = []
- for i in range(len(s) - 1):
- if s[i] != '|' or (s[i] == '|' and s[i - 1] != '|'):
- new_n += s[i]
- new_s = ''.join(new_n)
- if new_s[-1] != '|':
- new_s += s[-1]
- if new_s[-1] != '|':
- new_s += '|'
- s = new_s
- n = s.split('|')
- r = len(n[:-1])
- if not r % 6 == 0:
- i = r
- while i % 6:
- i += 1
- res = i - r
- for i in range(res):
- s += 'не указано|'
- else:
- Person.save = ''
- # print('file is empty')
- return s
- @staticmethod
- def change_file_for_valid(x):
- with open(x, 'w', encoding='utf-8') as file:
- file.write(Person.save)
- @staticmethod
- def read_db():
- Person.save = Person.test_file_for_valid_format('objects.txt')
- Person.change_file_for_valid('objects.txt')
- with open('objects.txt', encoding='utf-8') as file:
- s = file.read().strip().split('|')
- res = []
- for i in range(0, len(s) - 5, 6):
- res += [s[i: i + 6]]
- return res
- @staticmethod
- def create_objects():
- Person.instances.clear()
- s = Person.read_db()
- for i in s:
- obj = Person(*i)
- @staticmethod
- def draw_gui():
- root = Tk()
- root.geometry('600x650+200+100')
- root.title('Alexander`s Notebook 1.1')
- #root.iconbitmap('favicon.ico')
- root.configure(bg='beige')
- def change_spaces(s):
- s = s.replace('|', '/')
- return s
- def test_button():
- result = [e1.get(), e2.get(), e3.get(), e4.get(), e5.get(), e6.get()]
- result2 = list(map(lambda x: 'не указано' if x == '' else x, result))
- for i in range(len(result2)):
- result2[i] = change_spaces(result2[i])
- Person.data_from_entries = '|'.join(result2)
- e1.delete(0, END)
- e2.delete(0, END)
- e3.delete(0, END)
- e4.delete(0, END)
- e5.delete(0, END)
- e6.delete(0, END)
- Person.append_obj_to_file()
- Person.create_objects()
- def search_name():
- try:
- label7.config(foreground='green', justify='center', font=('verdana', 20))
- try:
- Person.create_objects()
- Person.search = e7.get()
- Person.output(label7, label8)
- e7.delete(0, END)
- except IndexError:
- label7.config(text='ошибка ввода')
- with open('objects.txt', 'w', encoding='utf-8') as file:
- file.write(Person.save)
- except FileNotFoundError:
- label7.config(text='Для создания файла с данными \nвведите первый контакт')
- Person.create_file()
- except:
- label7.config(text='Что-то пошло не так \nПерезапустите программу')
- label8.config(text='')
- def to_del_contact():
- x = e9.get()
- with open('objects.txt', encoding='utf-8') as file:
- s = file.read()
- if x in s:
- n = s.split('|')
- indexes = []
- deleted = []
- for i, f in enumerate(n):
- if f == x:
- indexes += [i]
- for i in indexes:
- if n[i - 1] == e8.get():
- deleted = n[i - 1: i + 5]
- break
- to_removed = '|'.join(deleted) + '|'
- del n[i - 1:i + 5]
- s = '|'.join(n)
- with open('removed.txt', 'a', encoding='utf-8') as file:
- file.write(to_removed)
- with open('objects.txt', 'w', encoding='utf-8') as file:
- file.write(s)
- e8.delete(0, END)
- e9.delete(0, END)
- def show_contacts():
- try:
- # сортировка по фамилиям
- with open('objects.txt', encoding='utf-8') as file:
- s = file.read()
- ns = s.split('|')[:-1]
- res = []
- for i in range(0, len(ns) - 5, 6):
- res += [ns[i: i + 6]]
- res.sort(key=lambda x: x[1])
- # сортировка однофамильцев по именам
- result = []
- def grouper(item):
- return item[1]
- for key, group_items in groupby(res, key=grouper):
- b = list(group_items)
- b.sort(key=lambda x: x[0])
- b = sum(b, [])
- result.append(b)
- result = sum(result, [])
- s = '|'.join(result) + '|'
- with open('objects.txt', 'w', encoding='utf-8') as file:
- file.write(s)
- # сортировка по фамилиям
- Person.create_objects()
- label7.config(foreground='blue', justify='left')
- contacts = ''
- if not Person.instances:
- label7.config(text='База контактов пуста')
- else:
- for i in Person.instances:
- contacts += f'{i.surname} {i.name}\n'
- label7.config(text=contacts, font=('verdana', 10))
- label8.config(text='')
- except:
- label7.config(text='')
- label8.config(text='Что-то пошло не так \nПерезапустите программу')
- def restore_contacts():
- Person.save = Person.test_file_for_valid_format('removed.txt')
- Person.change_file_for_valid('removed.txt')
- s = ''
- with open('removed.txt', encoding='utf-8') as file1:
- s = file1.read()
- # print(s)
- with open('objects.txt', 'a', encoding='utf-8') as file2:
- file2.write(s)
- with open('removed.txt', 'w', encoding='utf-8') as file3:
- file3.write('')
- label1 = Label(text='Имя', bg='beige', font=('verdana', 10))
- e1 = Entry(width=40)
- label2 = Label(text='Фамилия', bg='beige', font=('verdana', 10))
- e2 = Entry(width=40)
- label3 = Label(text='Пол', bg='beige', font=('verdana', 10))
- e3 = Entry(width=40)
- label4 = Label(text='Возраст', bg='beige', font=('verdana', 10))
- e4 = Entry(width=40)
- label5 = Label(text='Номер', bg='beige', font=('verdana', 10))
- e5 = Entry(width=40)
- label6 = Label(text='Адрес', bg='beige', font=('verdana', 10))
- e6 = Entry(width=40)
- button = Button(text='Добавить контакт', foreground='red', command=test_button)
- e7 = Entry(width=40)
- button2 = Button(text='Искомая фамилия', foreground='green', command=search_name)
- label7 = Label(text='Результат поиска', foreground='red', bg='beige', font=('verdana', 10))
- label8 = Label(justify=LEFT, bg='beige')
- label9 = Label(text="Удаление контактов", bg='beige', font=('verdana', 10))
- e8 = Entry(width=40)
- button3 = Button(text='Удалить контакт', command=to_del_contact)
- label10 = Label(text="Имя", bg='beige', font=('verdana', 10))
- label11 = Label(text="Фамилия", bg='beige', font=('verdana', 10))
- e9 = Entry(width=40)
- button5 = Button(text='Показать контакты', command=show_contacts)
- button4 = Button(text='Восстановить', command=restore_contacts)
- label1.place(x=120, y=10)
- e1.place(x=20, y=30)
- label2.place(x=105, y=50)
- e2.place(x=20, y=70)
- label3.place(x=120, y=90)
- e3.place(x=20, y=110)
- label4.place(x=110, y=130)
- e4.place(x=20, y=150)
- label5.place(x=115, y=170)
- e5.place(x=20, y=190)
- label6.place(x=115, y=210)
- e6.place(x=20, y=230)
- button.place(x=85, y=260)
- e7.place(x=20, y=300)
- button2.place(x=85, y=330)
- label7.place(x=320, y=10)
- label8.place(x=300, y=50)
- label9.place(x=85, y=360)
- label10.place(x=120, y=380)
- e8.place(x=20, y=405)
- label11.place(x=105, y=425)
- e9.place(x=20, y=445)
- button3.place(x=85, y=475)
- button4.place(x=90, y=510)
- button5.place(x=78, y=565)
- e1.focus()
- root.mainloop()
- except FileNotFoundError:
- Person.create_file()
- Person.create_removed()
- finally:
- Person.draw_gui()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement